EN · $ USD
FiveM Scripts and Development

How to Write Your First FiveM Script in Lua

7 min read Updated: OyunBulut technical team

In short

To write a FiveM script in Lua, create a folder under resources with fxmanifest.lua, client.lua and server.lua files. Commands and permission checks go on the server with RegisterCommand, while in-game actions like fixing a vehicle run on the client through events registered with RegisterNetEvent. Add the resource to server.cfg with an ensure line and test it with the restart command.

In this guide
  1. Before you start: What is a resource?
  2. Client vs. server: Which code runs where?
  3. Step 1: Create the folder and fxmanifest.lua
  4. Step 2: Write the command and permission check on the server
  5. Step 3: Write the events and repair logic on the client
  6. Step 4: Add it to server.cfg and grant permission
  7. Step 5: Test and read the errors
  8. Next steps to improve the script
  9. Write scripts faster with AI
  10. What's next

To write a FiveM script in Lua you need three things: an fxmanifest.lua file that defines the resource, a client file that runs on the player's PC, and a server file that runs on the server. In this guide we use those three pieces to build a real /tamir (repair) command from scratch, complete with a permission check and a cooldown. By the end you'll have a clean resource you can reuse as a template for your own scripts.

Before you start: What is a resource?

In FiveM, every add-on is called a resource. A resource is a package with its own folder under resources and a manifest file inside it. When the server starts, it reads the ensure kaynak_adi (resource name) lines in server.cfg and starts those resources. If you haven't worked with the folder structure and ensure before, our guide to installing FiveM scripts and the folder structure will make things easier.

A stable, always-on test server makes a big difference during development. You can experiment on your local PC, but to test with friends and see real network conditions, a small OB-FIVEM-2 FiveM server is enough as a dev environment; 4 GB of DDR5 RAM leaves comfortable room for a few test resources on their own.

Client vs. server: Which code runs where?

The most common mistake in FiveM scripting is putting code on the wrong side. The rule is simple:

  • Client side code runs in the player's game. Vehicles, the character (ped), on-screen text and key presses live here. The player can access and modify this code, so it can't be trusted.
  • Server side code runs on the server machine. Decisions such as permission checks, money, database access and cooldowns must be made here.
  • An event is a message between the two sides. The client messages the server with TriggerServerEvent, and the server messages the client with TriggerClientEvent.

For our /tamir command, the flow works like this: the player types the command, the server checks permission and cooldown, and if everything passes it sends a "repair the vehicle" message to the client. The functions (natives) that actually repair the vehicle run on the client, so the final step happens there.

Step 1: Create the folder and fxmanifest.lua

Create a folder named tamir under resources and add three files to it:

resources/
└── tamir/
    ├── fxmanifest.lua
    ├── client.lua
    └── server.lua

fxmanifest.lua tells FiveM which game the resource is for and which files run where:

fx_version 'cerulean'
game 'gta5'
lua54 'yes'

author 'Sizin Adiniz'
description 'Basit /tamir komutu'
version '1.0.0'

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

fx_version 'cerulean' is the current manifest version, and lua54 'yes' enables the Lua 5.4 runtime. When you have multiple files, you can use a list with wildcards, such as client_scripts { 'client/*.lua' }. For settings shared by both sides, add a shared_script 'config.lua' line.

Step 2: Write the command and permission check on the server

We define the command on the server because the answer to "can this player repair?" must come from a place the player can't tamper with. server.lua:

local BEKLEME_SURESI = 60 -- seconds
local sonKullanim = {}

RegisterCommand('tamir', function(source, args, rawCommand)
    local src = source

    -- if source is 0, the command was typed in the server console
    if src == 0 then
        print('[tamir] Bu komut oyun icinden kullanilmalidir.')
        return
    end

    -- ACE permission check
    if not IsPlayerAceAllowed(src, 'tamir.kullan') then
        TriggerClientEvent('tamir:bildirim', src, 'Bu komutu kullanma yetkiniz yok.')
        return
    end

    -- Cooldown
    local simdi = os.time()
    if sonKullanim[src] and simdi - sonKullanim[src] < BEKLEME_SURESI then
        local kalan = BEKLEME_SURESI - (simdi - sonKullanim[src])
        TriggerClientEvent('tamir:bildirim', src, ('Tekrar kullanmak icin %d saniye bekleyin.'):format(kalan))
        return
    end

    sonKullanim[src] = simdi
    TriggerClientEvent('tamir:uygula', src)
end, false)

-- Clear the table when the player leaves
AddEventHandler('playerDropped', function()
    sonKullanim[source] = nil
end)

Key points to note:

  • The last parameter of RegisterCommand is false, so the command is registered as open to everyone, but we check permission ourselves. That lets us show unauthorized players a meaningful message. If you passed true, FiveM would automatically look for the command.tamir ACE permission.
  • Copying source into a local src variable right away is a good habit; source can change inside nested functions and code that waits.
  • Because the cooldown table lives on the server, players can't bypass the timer even if they modify the client file.

Step 3: Write the events and repair logic on the client

client.lua listens for two events from the server: one for notifications and one for the actual repair.

local function bildir(mesaj)
    BeginTextCommandThefeedPost('STRING')
    AddTextComponentSubstringPlayerName(mesaj)
    EndTextCommandThefeedPostTicker(false, true)
end

RegisterNetEvent('tamir:bildirim', function(mesaj)
    bildir(mesaj)
end)

RegisterNetEvent('tamir:uygula', function()
    local ped = PlayerPedId()
    local arac = GetVehiclePedIsIn(ped, false)

    if arac == 0 then
        bildir('Bir aracin icinde olmalisiniz.')
        return
    end

    if GetPedInVehicleSeat(arac, -1) ~= ped then
        bildir('Tamir icin surucu koltugunda olmalisiniz.')
        return
    end

    SetVehicleFixed(arac)
    SetVehicleDeformationFixed(arac)
    SetVehicleEngineHealth(arac, 1000.0)
    SetVehicleBodyHealth(arac, 1000.0)
    SetVehicleDirtLevel(arac, 0.0)

    bildir('Araciniz tamir edildi.')
end)

-- Command suggestion in the chat box
CreateThread(function()
    TriggerEvent('chat:addSuggestion', '/tamir', 'Icinde bulundugunuz araci tamir eder')
end)

RegisterNetEvent marks an event as one that can be triggered over the network (from the server). Without this line, the client ignores events sent by the server. The driver's seat check matters too: the network owner of a vehicle entity is usually the driver, and for repair natives to sync correctly to other players, the player controlling the vehicle has to run them.

Step 4: Add it to server.cfg and grant permission

To start the resource and decide who can use it, add these lines to server.cfg:

ensure tamir

# grant repair permission to the admin group
add_ace group.admin tamir.kullan allow

# add a specific player to the admin group (example)
add_principal identifier.fivem:1234567 group.admin

Replace identifier.fivem:1234567 in the example with your own Cfx.re ID. For details on the ACE system, groups and permission inheritance, see our FiveM ACE permissions guide.

Step 5: Test and read the errors

  1. In the server console or txAdmin console, type refresh and then ensure tamir. After code changes, restart tamir is enough.
  2. In game, get into a vehicle, damage it a bit and type /tamir in chat.
  3. If nothing happens, open the F8 console in game; client errors show up there. Server errors appear as red lines in the server console.
  4. Try the command with an account that has no permission and confirm you get the "no permission" message. Type it twice in a row to verify the cooldown as well.

Common errors and what they mean:

SymptomLikely cause
Nothing happens when you type the commandThe resource isn't started; check the ensure tamir output in the console and look for typos.
"attempt to call a nil value"A function name is misspelled, or the code is on the wrong side (a client native in the server file).
Everyone gets "no permission"The add_ace or add_principal line is missing, or the identifier is wrong.
The repair shows for you but not for othersYou aren't the vehicle's owner; don't remove the driver's seat check.

Next steps to improve the script

This skeleton is easy to extend. Popular additions:

  • Config file: move the cooldown and messages into config.lua and load it with shared_script.
  • Paid repairs: if you use ESX or QBCore, check and deduct the player's money on the server side. Money transactions must never happen on the client.
  • Animation and duration: adding a few-second progress bar before the repair makes it feel more realistic on roleplay servers.
  • Logging: send who repaired and when to a Discord webhook.

For the full list of native functions and their parameters, the official FiveM natives reference should be your go-to resource.

Write scripts faster with AI

Once you know Lua syntax, the most time-consuming part is finding the right native and remembering framework functions. An AI assistant saves real time here: you can run requests like "add a QBCore money check to this command" or "what's causing this error" directly against your code. The AI Developer Pro plan, with 1M token capacity and usage statistics, is the entry level for someone writing their first scripts. We cover how to review AI-generated code separately in our guide to writing FiveM scripts with AI.

What's next

Once you install and run the tamir resource from this guide on your own server, you'll have put manifests, the client/server split, events and commands into practice. Testing your own scripts on a separate test server before going live is the safest approach. OyunBulut's FiveM server plans come ready with automatic setup, so you can spin up a test environment in minutes and try your code with real players; and if you want to speed up the coding side, you can start with AI Developer Pro.

Frequently asked questions

What language are FiveM scripts written in?

FiveM officially supports Lua, JavaScript and C#. Most community scripts are written in Lua, so Lua is the most practical choice for beginners.

What's the difference between fxmanifest.lua and __resource.lua?

__resource.lua is the legacy manifest format. New resources should use fxmanifest.lua, which includes the fx_version and game lines.

Do I need to restart the server after changing code?

No. Typing restart resource_name in the server or txAdmin console is enough. If you added a new resource, run the refresh command first.

Why shouldn't I do money or permission checks on the client?

Client code runs on the player's PC and can be modified with cheat tools. Decisions like money, permissions and cooldowns must always be made on the server side.

Where can I see client errors?

Client errors appear in the in-game console you open with F8. Server-side errors are listed in the server console or the txAdmin live console.

For this guide

Recommended plans

AI Developer Pro cuts the time a developer writing their first Lua scripts spends hunting for natives and debugging, while OB-FIVEM-2 gives you a separate test server where you can try your code with real players.

AI Developer Pro

Recommended

1M tokens / month — usage percentage and stats tracking.

$9.83/mo

+VAT

  • LocationIstanbul
  • 1M tokens
  • % + stats
  • Automatic Setup

4 GB DDR5 RAM

$11.01/mo

+VAT

  • Processor2 CPU
  • Disk50 GB NVMe
  • LocationIstanbul
  • DDoS
  • Automatic Setup

FiveM Services — tüm paketler FiveM — tüm paketler

Need help with setup?

Send questions about your service in a support ticket, and our technical team will walk you through it step by step.