ServersYour serverFor creatorsForumFAQDocumentationSecurity
Download the launcher
  1. Documentation
  2. Shared functions
  3. setTimer

Function

setTimer

Server and clientTimersIdentical to MTA:SA

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 to 50.
repeatsint
How many times to call the function. 0 means 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