JavaScript Basics
JavaScript is a modern alternative to Lua for FiveM scripting. FiveM runs JavaScript separately in client and server contexts. Keep shared data serializable, use runtime-specific natives only on the correct side, and avoid blocking loops so other resource callbacks continue to execute.
Variables
const name = 'John'
let age = 25
var isActive = trueFunctions
function greet(name) {
return `Hello, ${name}`
}
const message = greet('World')Objects
const player = {
name: 'John',
age: 25,
items: ['sword', 'shield'],
}
console.log(player.name) // "John"
console.log(player.items[0]) // "sword"FiveM Functions
// Get player
const playerId = GetPlayerPed(-1)
// Get player coords
const coords = GetEntityCoords(playerId)
// Trigger event
emit('myevent', data)Async/Await
async function fetchData() {
const response = await fetch('https://api.example.com/data')
const data = await response.json()
return data
}Common Patterns
Debouncing
let lastTime = 0
function debouncedAction() {
const currentTime = GetGameTimer()
if (currentTime - lastTime < 1000) {
return
}
lastTime = currentTime
// Do action
}Threading
setTick(() => {
// Do something every frame
})