CreateThread, Wait, and the tick budget
Here's the deal: a loop that never pauses freezes the whole game. CreateThread starts a background loop, and Wait(ms) is the line inside it that pauses and hands the game back control. No Wait, total freeze. The other half of the skill is picking the pause length: too short and you burn frame time for nothing, too long and you miss the keypress or the feature feels laggy.
Build it
Make the resource folder
Inside your server's resources folder, create this folder:
resources/qu_createthread_and_wait
Create the files
Create this exact file layout:
resources/qu_createthread_and_wait/
fxmanifest.lua
client.lua
Write fxmanifest.lua
Open fxmanifest.lua and paste this:
fx_version 'cerulean'
game 'gta5'
client_script 'client.lua'
Older tutorials add a lua54 'yes' line here. As of June 2025 that setting is deprecated and ignored: Lua 5.4 is now the only Lua runtime, so you leave it out.
Write the lesson code
Open client.lua and paste this:
local enabled = false
RegisterCommand('threadtoggle', function()
enabled = not enabled
print('[qu_createthread_and_wait] enabled ' .. tostring(enabled))
end, false)
-- Slow tick: a heartbeat that only needs to fire occasionally.
CreateThread(function()
while true do
Wait(5000)
if enabled then
print('[qu_createthread_and_wait] slow tick')
end
end
end)
-- Fast poll: must run every frame to catch the exact frame E is pressed.
CreateThread(function()
while true do
Wait(0)
if enabled and IsControlJustPressed(0, 38) then
print('[qu_createthread_and_wait] E pressed')
end
end
end)
Start and test it
Open server.cfg and add this line:
ensure qu_createthread_and_wait
Save, then run this in the txAdmin Live Console:
restart qu_createthread_and_wait
Now join the server. Run /threadtoggle in chat, or type threadtoggle without a slash in F8. Wait a few seconds for the slow tick, then press E.
Chat: /threadtoggle
F8: threadtoggle
The order of the last two lines depends on your timing. The point is that both threads are alive at once: one fires roughly every five seconds on its own, the other fires the instant you tap E. Two loops, two different Wait values, one resource.
Keep reading the full lesson
Sign in to start, then unlock every step of this lesson and the full FiveM School with a membership.
- How it works
- If something went wrong
- What you can do now
- Try it yourself
The remainder of CreateThread and Wait is available to FiveM School members.