Skip to content

SetTimer

Calls a function after a delay, once or repeatedly.

The function runs on the simulation thread in the tick the timer falls due - the same place a callback runs, so it may call anything a callback may. A repeating timer runs every ms milliseconds until KillTimer; a one-shot timer runs once and is gone. Timers belong to the mode: a reload discards them. The shortest interval is one tick (about 33 ms).

SetTimer(fn, ms [, repeating])
Parameter Type
fn function the function to call (no arguments)
ms number the delay, and the interval of a repeating timer, in milliseconds
repeating boolean true repeats every ms; false or nothing fires once (optional)

number - the timer’s id, for KillTimer

-- a countdown: "3", "2", "1", "Fight!"
local left = 3
local timer
timer = SetTimer(function()
if left > 0 then
GameTextForAll(tostring(left), 900)
left = left - 1
else
KillTimer(timer)
GameTextForAll("Fight!", 1500)
end
end, 1000, true)
-- once, in twenty seconds
SetTimer(function() DestroyEntity(corpse) end, 20000)

KillTimer · OnTick · GetServerTime · the Server and timers group of the index