Threads and timers
Threads are coroutines that the host resumes every tick. Wait pauses only the current thread.
CreateThread
Section titled “CreateThread”CreateThread(fn)Run a function as its own thread, starting with the next tick. Server and client.
Use threads for loops that run over time. Always call Wait inside a loop, otherwise the script exceeds its instruction budget and is stopped.
| Parameter | Type | Description |
|---|---|---|
fn |
fun() |
Body of the thread. |
CreateThread(function() while true do Wait(1000) print('one second passed') endend)Compared to FiveM: Same as Citizen.CreateThread in FiveM.
Wait(ms?)Pause the current thread. Server and client.
Only works inside a thread, event handler or timeout. The rest of the game keeps running.
| Parameter | Type | Description |
|---|---|---|
ms (optional) |
integer |
Milliseconds to wait. 0 or nothing waits until the next tick. |
Wait(500)Compared to FiveM: Same as in FiveM. Not allowed inside an export called from another resource.
SetTimeout
Section titled “SetTimeout”SetTimeout(ms, fn)Run a function once after a delay. Server and client.
| Parameter | Type | Description |
|---|---|---|
ms |
integer |
Delay in milliseconds. |
fn |
fun() |
Function to run. It runs as a thread and may call Wait. |
SetTimeout(5000, function() print('five seconds later')end)Compared to FiveM: Same as in FiveM.
Citizen.CreateThread
Section titled “Citizen.CreateThread”Citizen.CreateThread(fn)Same as CreateThread. Kept for ported FiveM scripts. Server and client.
| Parameter | Type | Description |
|---|---|---|
fn |
fun() |
Body of the thread. |
Compared to FiveM: Same as in FiveM.
Citizen.Wait
Section titled “Citizen.Wait”Citizen.Wait(ms?)Same as Wait. Kept for ported FiveM scripts. Server and client.
| Parameter | Type | Description |
|---|---|---|
ms (optional) |
integer |
Milliseconds to wait. |
Compared to FiveM: Same as in FiveM.
Citizen.SetTimeout
Section titled “Citizen.SetTimeout”Citizen.SetTimeout(ms, fn)Same as SetTimeout. Kept for ported FiveM scripts. Server and client.
| Parameter | Type | Description |
|---|---|---|
ms |
integer |
Delay in milliseconds. |
fn |
fun() |
Function to run. |
Compared to FiveM: Same as in FiveM.