Code Optimization
Optimize your code for better server performance.
Avoid while(true) Loops
Bad
while true do
Wait(0)
-- Do something every frame
endGood
CreateThread(function()
while true do
Wait(1000) -- Wait 1 second
-- Do something
end
end)Throttling
Prevent excessive function calls:
local lastCall = 0
function throttledAction()
local currentTime = GetGameTimer()
if currentTime - lastCall < 1000 then
return
end
lastCall = currentTime
-- Do action
endCaching Natives
Cache expensive native calls:
-- Bad: Called every frame
local coords = GetEntityCoords(PlayerPedId())
-- Good: Cache and update periodically
local cachedCoords = vector3(0, 0, 0)
CreateThread(function()
while true do
Wait(100)
cachedCoords = GetEntityCoords(PlayerPedId())
end
end)Best Practices
- Use appropriate Wait() times
- Cache expensive operations
- Throttle frequent calls
- Profile your code
Last updated on