Your First FiveM Lua Script: A Developer's Starting Guide

Published on

Learn to write FiveM Lua scripts from scratch. Covers FiveM's client/server architecture, native functions, events, and building your first custom resource.

Written by Space-Node Team – Infrastructure Team – 15+ years combined experience in game server hosting, VPS infrastructure, and 24/7 streaming solutions. Read author bio →

FiveM scripting is the most in-demand skill in the GTA V modding community. If you can write Lua scripts for FiveM, you can build custom features, sell resources, or just make your own server unique.

Lua script development for FiveM

FiveM Architecture

FiveM scripts run in two environments:

Client-side (runs on each player's computer):

  • Controls UI, visual effects, local vehicle behavior
  • Can access GTA V native functions for the player's game
  • Files named client.lua or registered as client scripts

Server-side (runs on the server):

  • Handles data, validation, and logic
  • Communicates with the database
  • Files named server.lua or registered as server scripts

They communicate via events - messages sent between client and server.

Your First Resource

Create a folder in your server's resources directory:

resources/[custom]/my-first-script/
    fxmanifest.lua
    client.lua
    server.lua

fxmanifest.lua

Every resource needs a manifest:

fx_version 'cerulean'
game 'gta5'

author 'YourName'
description 'My first FiveM script'
version '1.0.0'

client_script 'client.lua'
server_script 'server.lua'

client.lua - Hello World

-- Runs when the resource starts
CreateThread(function()
    while true do
        Wait(0)
        -- Draw text on screen
        SetTextFont(0)
        SetTextScale(0.5, 0.5)
        SetTextColour(255, 255, 255, 255)
        SetTextEntry("STRING")
        AddTextComponentString("Hello from my first script!")
        DrawText(0.5, 0.01)
    end
end)

This draws text on the player's screen every frame. Not very useful, but it proves your script is running.

Understanding Threads

FiveM uses cooperative multitasking with CreateThread and Wait:

CreateThread(function()
    while true do
        Wait(0)    -- Run every frame (0ms delay)
        -- Heavy processing, careful with this
    end
end)

CreateThread(function()
    while true do
        Wait(1000)  -- Run every second
        -- Light checks, timers, etc.
    end
end)

Important: Wait(0) runs every frame (60+ times per second). Use it only when you need frame-by-frame updates (drawing, input). For everything else, use longer wait times to save CPU.

Events: Client-Server Communication

Server to Client

-- server.lua
RegisterCommand('heal', function(source)
    TriggerClientEvent('myScript:heal', source)
end)

-- client.lua
RegisterNetEvent('myScript:heal')
AddEventHandler('myScript:heal', function()
    local ped = PlayerPedId()
    SetEntityHealth(ped, GetEntityMaxHealth(ped))
end)

Client to Server

-- client.lua
RegisterCommand('report', function(source, args)
    local message = table.concat(args, ' ')
    TriggerServerEvent('myScript:report', message)
end)

-- server.lua
RegisterNetEvent('myScript:report')
AddEventHandler('myScript:report', function(message)
    local source = source
    print('Report from ' .. GetPlayerName(source) .. ': ' .. message)
end)

Native Functions

GTA V has thousands of native functions that FiveM exposes. Common ones:

| Function | Purpose | |----------|---------| | PlayerPedId() | Get current player entity | | GetEntityCoords(ped) | Get position | | SetEntityCoords(ped, x, y, z) | Teleport | | GetVehiclePedIsIn(ped) | Get current vehicle | | CreateVehicle(model, x, y, z, h) | Spawn vehicle | | GiveWeaponToPed(ped, hash, ammo) | Give weapon |

Full reference: FiveM Native Reference

Best Practices

Always validate on the server. Never trust client data. If a client event says "give me $1,000,000," the server should verify the player earned it.

Use Wait() appropriately. Scripts with Wait(0) everywhere tank client and server performance.

Namespace your events. Use prefixes like myScript:eventName to avoid conflicts with other resources.

Test your scripts on a local FiveM server before deploying to production. When you're ready for players, Space-Node FiveM hosting runs custom scripts smoothly on NVMe SSD with the Ryzen 9 7950X3D.

Space-Node Team

About the Author

Space-Node Team – Infrastructure Team – Experts in game server hosting, VPS infrastructure, and 24/7 streaming solutions with 15+ years combined experience.

Since 2023
500+ servers hosted
4.8/5 avg rating

Our team specializes in Minecraft, FiveM, Rust, and 24/7 streaming infrastructure, operating enterprise-grade AMD Ryzen 9 hardware in Netherlands datacenters. We maintain GDPR compliance and ISO 27001-aligned security standards.

View Space-Node's full team bio and credentials →

Launch Your VPS Today

Get started with professional VPS hosting powered by enterprise hardware. Instant deployment and 24/7 support included.

Your First FiveM Lua Script: A Developer's Starting Guide