Tutorial 2026-03-05

How to Optimize Your FiveM Scripts for 0ms Resmon

TDYSKY

TDYSKY

Founder & Lead Developer at Agency Scripts

Understanding Resmon and Script Performance

The resmon command in FiveM displays real-time resource performance metrics, showing how many milliseconds each script consumes per server tick. A well-optimized script should show 0.00ms in resmon most of the time, spiking briefly only when actively processing events. Scripts that consistently show high resmon values degrade server performance, increase player desync, and can cause the dreaded server lag that drives communities away. Profiling your scripts regularly is the first step toward maintaining a smooth server.

Thread Management and Wait Times

The biggest performance killer in FiveM scripts is improper thread management. Using Citizen.Wait(0) inside a loop means your code runs every single frame, which is roughly 60 times per second. Most tasks do not need this frequency. Increase your wait times to Wait(500) or Wait(1000) for checks that only need to run periodically, like proximity detection for markers or zone checks. For distance-based systems, use a tiered approach where you check at longer intervals when far from points of interest and shorter intervals when nearby.

Disabling Unnecessary Threads

Instead of running threads continuously, consider activating them only when needed. Use events or state changes to start and stop processing loops. For example, a fishing script does not need to run its casting loop when the player is not near water. Track an active state variable and exit the thread with return when the feature is not in use. This pattern can reduce your script from constant 0.50ms to 0.00ms during idle periods, which is the difference between a script that scales and one that cripples your server.

Caching and Avoiding Redundant Calls

Native function calls in FiveM have overhead, especially when called repeatedly inside tight loops. Cache the results of calls like GetEntityCoords, GetPlayerPed, and GetPlayerServerId at the start of your loop iteration rather than calling them multiple times within the same cycle. Store static data like configuration values, marker positions, and blip coordinates in local variables at script load time. This simple technique can cut your script execution time dramatically and is one of the most impactful optimizations you can make.

Mathematical Optimizations

Distance calculations are among the most common operations in FiveM scripts. Replace the standard #(coords1 - coords2) distance check with squared distance comparisons when you only need relative distance. Since square root calculation is expensive, comparing squared distances against squared thresholds achieves the same result without the costly math operation. Additionally, avoid calling GetDistanceBetweenCoords native when you can calculate distance in Lua, as native calls carry additional overhead compared to pure Lua operations.

Entity and Blip Management

Creating entities, blips, and markers has both rendering and network costs. Remove blips and entities when they are no longer needed rather than leaving them in the game world. Use DrawMarker only when the player is within rendering distance, not globally across the entire map. For scripts that manage many entities, implement spatial partitioning to only process nearby objects. Consider using streaming systems that create and destroy entities based on player proximity rather than loading everything at once when the resource starts.

Share this article

Ready to upgrade your server?

Check out our premium FiveM scripts in the Agency Scripts store or join our Discord community for support and updates.