- Documentation
- Shared functions
- setTimer
Function
setTimer
Calls a function after a given time, once, several times or forever.
Syntax
timer setTimer(function fn, int interval, int repeats [, ...])Arguments
fnfunction- The function to call.
intervalint- Interval in milliseconds. At least
50, a shorter time is raised to50. repeatsint- How many times to call the function.
0means forever. ...any, optional- Arguments passed to the function on every call.
Returns
A timer handle. Stop it with killTimer(timer).
Example
A chat announcement every 10 minutes and a countdown before a race starts.
-- forever, every 10 minutes
setTimer(function()
outputChatBox("Join our Discord!", root, 255, 122, 34)
end, 10 * 60 * 1000, 0)
-- 3 times, once a second, with an argument
local left = 3
setTimer(function(name)
outputChatBox(name .. ": starting in " .. left .. "...")
left = left - 1
end, 1000, 3, "Race")
Notes
The timer belongs to the resource. When the resource stops, all its timers are removed, there is no need to clean them up by hand.
Do not draw on a timer. For drawing on screen use onClientRender, not a 50 ms timer. Timers are not in sync with the game's frames.
See also
spawnPlayerSpawns a player at the given place: position, rotation, skin, interior and dimension.getElementPositionReturns an element's position in the world: a player, vehicle, object or marker.isTimerChecks whether a value is an active timer. A one-shot timer stops being active after it fires.resetTimerRestarts counting the timer's interval from this moment.