SQZ Scripts
Search…
Optimalization
I am going to make simple optimalization snippets for FiveM optimalization to save your server performance.

If you will follow these steps you will be fine

  1. 1.
    Use Lua math, functions ... instead of FiveM natives/functions
  2. 2.
    Use FiveM functions/Natives instead of your framework functions
  3. 3.
    Use your framework functions
How does that work? It is easy, just go 1 - 3, if you can not use 1., use the 2., if you can not use the point 3. Why? The first is the faster one and the 3. is the slowest one in the most cases. Why to use framework function if raw Lua can do it?

Counting Distance about 2 points

This seems to be the biggest problem on most servers? Why? It is so f*ckin' easy to optimise getting distance between 2 places.
The least correct
More correct
The most correct
1
Citizen.CreateThread(function ()
2
while true do
3
Wait(0)
4
5
local coords = GetEntityCoords(GetPlayerPed(-1))
6
7
for k,v in pairs(Config.Zones) do
8
if(v.Type ~= -1 and GetDistanceBetweenCoords(coords, v.Pos.x, v.Pos.y, v.Pos.z, true) < Config.DrawDistance) then
9
DrawMarker(v.Type, v.Pos.x, v.Pos.y, v.Pos.z, 0.0, 0.0, 0.0, 0, 0.0, 0.0, v.Size.x, v.Size.y, v.Size.z, v.Color.r, v.Color.g, v.Color.b, 100, false, true, 2, false, false, false, false)
10
end
11
end
12
end
13
end)
14
15
-- Lets say that we have defined all the locations and the Config with DrawDistance
16
-- It is incorrect, because "GetDistanceBetweenCoords" is "slow" in comparison with lua math
Copied!
1
Citizen.CreateThread(function ()
2
while true do
3
Wait(0)
4
5
local coords = GetEntityCoords(GetPlayerPed(-1))
6
7
for k,v in pairs(Config.Zones) do
8
if(v.Type ~= -1 and #(coords, vector3(v.Pos.x, v.Pos.y, v.Pos.z)) < Config.DrawDistance) then
9
DrawMarker(v.Type, v.Pos.x, v.Pos.y, v.Pos.z, 0.0, 0.0, 0.0, 0, 0.0, 0.0, v.Size.x, v.Size.y, v.Size.z, v.Color.r, v.Color.g, v.Color.b, 100, false, true, 2, false, false, false, false)
10
end
11
end
12
end
13
end)
14
15
-- This is way more optimized, because it uses Lua math in stead of FiveM native
16
-- But it is still not a lot optimised
Copied!
1
Citizen.CreateThread(function ()
2
while true do
3
Wait(0)
4
5
local coords = GetEntityCoords(GetPlayerPed(-1))
6
local sleep = true
7
for k,v in pairs(Config.Zones) do
8
if(v.Type ~= -1 and #(coords, vector3(v.Pos.x, v.Pos.y, v.Pos.z)) < Config.DrawDistance) then
9
sleep = false
10
DrawMarker(v.Type, v.Pos.x, v.Pos.y, v.Pos.z, 0.0, 0.0, 0.0, 0, 0.0, 0.0, v.Size.x, v.Size.y, v.Size.z, v.Color.r, v.Color.g, v.Color.b, 100, false, true, 2, false, false, false, false)
11
end
12
end
13
if sleep then
14
Wait(500)
15
end
16
end
17
end)
18
-- This should be way more optimized and run at 0.01ms without any performance impacts
Copied!