๐Script Optimization
Learn how to write optimized code!
General Practices
Localize Functions and Variables
Lua accesses local variables and functions faster than global ones. Always use
localwhen declaring variables or functions unless explicitly required to be global
myVariable = false -- Don't use this
local myVariable = false -- Use this
function someFunction() -- Don't use this
print('Im a global function!')
end
local function someFunction() -- Use this
print('Im a local function!')
endUse Table Indexing Instead of table.insert
table.insertadds slight overhead; directly assigning a value is more efficient
function someFunction()
local table = {}
table.insert(table, {}) -- Don't use this
table[#table+1] = {} -- Use this
endSimplify Conditional Checks
Use
if something theninstead ofif something ~= nilto check for bothnilandfalse
Keep Functions Universal
Write functions and events that can handle multiple scenarios by passing parameters. This increases code reusability
Short Returns
Use short returns to exit a function early if conditions aren't met. This keeps code cleaner and avoids unnecessary nested
ifblocks
Avoid Re-Creating Tables or Variables Repeatedly
Instead of creating tables or variables repeatedly inside loops or frequently called functions, initialize them once and reuse
Use nil to Free Up Memory
Assign unused variables to
nilto let Luaโs garbage collector free the memory
Avoid Hardcoding
Centralize configurable values (like coordinates, item names, or payment amounts) into a
config.luafile for easier management
Example config.lua:
Logging and Debugging
Use a debug mode toggle in your scripts to enable or disable logs dynamically without removing them. You could even add it as an option in your
config.luafrom above!
Track Performance
Measure execution time for performance-critical sections using
os.clock()or FiveM natives
Avoid Overusing Network Events
Use shared state (e.g., via
state bagsorentity states) when frequent data synchronization is needed, rather than spammingTriggerEventorTriggerServerEvent
Optimize Data Transmission
Only send the data you need, not entire tables or large payloads
Code Readability
Comment Your Code
Include comments to explain logic, especially for complex or non-obvious sections
Organize Your Script
Structure your script logically, separating variables, functions, event handlers, and core logic into distinct sections
Folder Structure
Break scripts into smaller, manageable pieces instead of writing everything in one file
Native Usage
Always replace
GetPlayerPed(-1)withPlayerPedId()
Always replace
GetDistanceBetweenCoordswith lua math aka#(vector3 - vector3)
Loops
Control your while loops and when they run
If you do have to create a thread that includes a "while" loop, always avoid using "while true do" if able. If you have to use this, follow the next tip, and it wonโt impact performance as much
Control your thread times by using a variable that changes the wait time retroactively. So you can set the thread wait time to say 1000ms which checks for your if statement every second and if it makes it into the statement you can lower the wait time by just changing the variable value. Wait(sleep)
If you have job specific loops, make sure they only apply to players with that job. There's no reason for someone who is not a cop to be running a loop on their machine that does not apply to them
Security
A surplus amount of security in a code is not a bad thing. Don't be afraid to add in multiple if checks or create random variables to pass through your events
Never do any type of transaction with the player regarding money or items on the client side of a resource
Event Handlers
When setting variables inside your resource, handlers come in especially handy due to not needing to constantly run checks
Last updated
Was this helpful?
