Skip to content
GAMPEONCreate
New project
Preview: this works today, but details can still change.

Threads and timers

Scripts must never block the game. Long-running work happens in threads: functions that pause with Wait and continue on a later tick.

CreateThread(function()
while true do
Wait(1000) -- pause one second; the game keeps running
print('tick')
end
end)
  • CreateThread(fn) starts fn on the next tick.
  • Wait(ms) pauses the current thread. Wait(0) waits until the next tick.
  • SetTimeout(ms, fn) runs fn once after a delay.
  • Event handlers run as threads too, so they may call Wait.

Every time a thread runs, it gets a budget of instructions (50 million by default). A loop without Wait runs out of budget and the runtime stops that run with an error. Other resources keep running.

-- Wrong: never yields
CreateThread(function()
while true do end
end)
-- Right
CreateThread(function()
while true do
Wait(0)
end
end)

Wait at the top level of a script fails: Wait can only be called inside a thread or event handler. Wrap the code in CreateThread.

GetGameTimer() returns milliseconds since the runtime started:

local started = GetGameTimer()
Wait(250)
print(GetGameTimer() - started) -- about 250

The FiveM names Citizen.CreateThread, Citizen.Wait and Citizen.SetTimeout work as well.

Report a problem with this pageOther ways to give feedback