timer

Run code later, or again and again.

Timers count real seconds and keep running in menus and while paused (check game.paused() if that matters). Timer functions get their own id, so they can cancel themselves.

Functions

# timer.after(seconds, fn) → integer

Runs fn once after a delay.

ParameterType
secondsnumber
fnfunctionfn(id)
Returns
integertimer id
timer.after(10, function() world.spawnNear("trex", 3000) end)

# timer.every(seconds, fn) → integer

Runs fn again and again. A failing repeating timer is stopped.

ParameterType
secondsnumberinterval
fnfunctionfn(id)
Returns
integertimer id
local wave = 0
timer.every(60, function(id)
  wave = wave + 1
  for i = 1, wave do world.spawnNear("velociraptor", 2500) end
  if wave == 10 then timer.cancel(id) end
end)

# timer.cancel(id) → boolean

Stops a timer.

ParameterType
idinteger
Returns
booleantrue when it was running

# timer.now() → number

Seconds since the scripts started.

Returns
numberseconds