Lua Basics
Lua is the most common scripting language for FiveM resources.
Variables
local name = "John"
local age = 25
local isActive = trueFunctions
function greet(name)
return "Hello, " .. name
end
local message = greet("World")Tables
local player = {
name = "John",
age = 25,
items = {"sword", "shield"}
}
print(player.name) -- "John"
print(player.items[1]) -- "sword"FiveM Functions
-- Get player
local playerId = GetPlayerPed(-1)
-- Get player coords
local coords = GetEntityCoords(playerId)
-- Trigger event
TriggerEvent('myevent', data)Common Patterns
Debouncing
local lastTime = 0
function debouncedAction()
local currentTime = GetGameTimer()
if currentTime - lastTime < 1000 then
return
end
lastTime = currentTime
-- Do action
endThreading
CreateThread(function()
while true do
Wait(1000)
-- Do something every second
end
end)Last updated on