๐Ÿš€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 local when 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!')
end

Use Table Indexing Instead of table.insert

  • table.insert adds 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
end

Simplify Conditional Checks

  • Use if something then instead of if something ~= nil to check for both nil and false

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 if blocks

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 nil to let Luaโ€™s garbage collector free the memory

Avoid Hardcoding

  • Centralize configurable values (like coordinates, item names, or payment amounts) into a config.lua file 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.lua from 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 bags or entity states) when frequent data synchronization is needed, rather than spamming TriggerEvent or TriggerServerEvent

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) with PlayerPedId()

  • Always replace GetDistanceBetweenCoords with 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?