Guide
How to Optimize FiveM Lua Scripts and Reach 0.00ms Idle
On a FiveM server, every client script that ticks every frame costs players performance, and the cost adds up across dozens of resources. A well-written resource should sit at 0.00ms when idle and only use time when something actually happens. These are the habits that get you there.
Measure first with resmon
Open the F8 console and run resmon 1 to see the CPU time per resource. Anything that shows constant non-zero values while the player is standing still is doing work every frame. Fix the biggest ones first. For deeper analysis, the built-in profiler commands can record where time goes.
1. Stop using Wait(0) when you do not need it
A loop with Wait(0) runs every frame. Most checks do not need that.
-- Bad: runs every frame, forever
CreateThread(function()
while true do
Wait(0)
local ped = PlayerPedId()
local coords = GetEntityCoords(ped)
-- check distance to something
end
end)
2. Use a dynamic wait based on distance
Run fast only when the player is close to the thing you care about, and sleep otherwise.
-- Better: sleeps long when far away
CreateThread(function()
while true do
local sleep = 1000
local ped = PlayerPedId()
local dist = #(GetEntityCoords(ped) - target)
if dist < 20.0 then
sleep = 0
-- draw marker, handle input
elseif dist < 100.0 then
sleep = 250
end
Wait(sleep)
end
end)
3. Prefer events and zones over polling
If something happens at a location, use a zone or a point instead of checking distance yourself. Libraries such as ox_lib provide zones and points that only run code when the player enters or leaves. Likewise, react to an event, not a loop that asks "did it change yet?".
4. Cache values you read often
PlayerPedId() and similar natives are cheap but not free when called thousands of times. Read them once per second into a local variable and reuse it. Store frequently used natives in locals instead of looking up globals in a hot loop.
5. Avoid creating garbage
Creating new tables and strings every frame forces the garbage collector to work. Reuse tables where you can and build strings only when the content changes.
6. Keep database calls off the hot path
- Use the async functions in oxmysql so the server thread is not blocked.
- Never query the database on every tick. Load once, keep it in memory, and write back on a timer or on change.
- Add indexes to columns you filter on, such as identifiers and plates.
- Batch writes where possible, and use cooldowns for repeated actions.
7. Respect the client and server split
Do expensive validation on the server, and keep client scripts for presentation and input. Never trust the client for anything that affects money, items or permissions.
A real example
BSM Weapon Control is a weapon disarm resource I wrote for ESX, QBCore, Qbox and standalone servers. It supports radius control, ox_inventory items, safe zones and MySQL cooldowns, and it is built around these ideas so that it idles at 0.00ms.
Frequently asked questions
What is a good resmon value for a FiveM script?
Idle should read 0.00ms. Short spikes during an action are fine; constant 0.05ms or more while standing still usually means a per-frame loop to fix.
Is Wait(0) always bad?
No. Use it only while you must run every frame, for example drawing a marker or handling a key press near an object, and stop the loop when the player moves away.