Skip to main content
TRACK B·FIVEM FUNDAMENTALS·Verified June 2026 · Lua 5.4 · ox_lib 3.x
Learning with an AI assistant?
Copies this lesson plus 2026 ground rules (no lua54 'yes', Cfx.re Portal, correct callback signatures) as a ready-to-paste mentor prompt.

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.

You'll learn
What CreateThread actually creates, why Wait is mandatory inside loops, and how to pick the right wait time per use case
Time
~15 minutes
Prereqs
Loops and natives
Outcome
You can split client work across coroutines and keep polling from consuming unnecessary client frame time
Quasar School: loops, threads, and client/server comms.
BEFORE YOU START

Build it

Make the resource folder

The server has one folder for this lesson.

Inside your server's resources folder, create this folder:

code
resources/qu_createthread_and_wait

Create the files

Every file named in the manifest exists.

Create this exact file layout:

code
resources/qu_createthread_and_wait/
fxmanifest.lua
client.lua

Write fxmanifest.lua

FiveM knows which files to load.

Open fxmanifest.lua and paste this:

code
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

The topic is now represented by runnable code.

Open client.lua and paste this:

code
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

The expected proof appears in the F8 console.

Open server.cfg and add this line:

code
ensure qu_createthread_and_wait

Save, then run this in the txAdmin Live Console:

code
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.

code
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.

Still ahead in this lesson
  • 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.