shared
CreateThread / Citizen.CreateThread
FiveM shared native documentation with a Lua example.
Summary
Starts a new coroutine (thread) so code can loop and yield with Wait() without blocking the rest of the resource. The backbone of almost every FiveM script.
Returns
void
Parameters
handlerfunctionThe function to run on the new thread.Example
CreateThread(function()
while true do
Wait(1000) -- every second
-- do periodic work
end
end)Common pitfalls
- Every loop needs a Wait() or it freezes the thread and causes hitches.
- Use separate threads for separate concerns instead of one giant loop.
- Wait(0) runs every frame; use larger intervals when you don't need per-frame updates.
Related natives
Related guides
FiveM scripts done right
Optimized, dual-framework scripts for QBCore and ESX from Viper Development — escrow protected, delivered instantly to your Cfx portal.
Browse Scripts →