When learning FiveM development, one of the first concepts you’ll encounter is events. Events are the backbone of communication in FiveM—they allow your client scripts and server scripts to talk to each other, keep players synchronized, and make your server interactive.
In this post, we’ll cover:
- What FiveM events are
- The difference between client and server events
- How to create and use events in your scripts
- Best practices for working with events
What Are Events in FiveM?
In simple terms, events in FiveM are messages that can be sent and received between scripts. They allow you to trigger actions when something happens, such as:
- A player joins the server
- A player enters a vehicle
- A ped or object is spawned
- A client sends information to the server (or vice versa)
Events are a core part of networking in FiveM, making it possible to synchronize the game world across all players.
Client Events vs. Server Events
FiveM splits scripts into two main types:
- Client Events
- Run on the player’s game.
- Handle local actions like drawing UI, detecting key presses, or spawning effects.
- Example: Showing a notification when a player presses a button.
- Server Events
- Run on the central server.
- Handle global actions like saving player data, giving items, or spawning entities that everyone can see.
- Example: Adding money to a player’s account when they complete a mission.
Events connect the two, letting you send data and commands back and forth.
Common Event Functions in FiveM
Here are the main event functions you’ll use in FiveM scripting:
- RegisterNetEvent – Allows you to listen for a custom event.
- AddEventHandler – Defines what happens when the event is triggered.
- TriggerServerEvent – Sends an event from the client to the server.
- TriggerClientEvent – Sends an event from the server to one or more clients.
Example: Creating a Simple Event
Here’s a basic example of how client and server scripts can communicate using events.
Client Script
RegisterCommand("pay", function(source, args)
local amount = tonumber(args[1])
if amount then
TriggerServerEvent("playerPay", amount)
else
print("Usage: /pay [amount]")
end
end)
Server Script
RegisterNetEvent("playerPay")
AddEventHandler("playerPay", function(amount)
local src = source
-- Here you could add logic like checking if the player has enough money
print("Player " .. src .. " tried to pay $" .. amount)
TriggerClientEvent("chat:addMessage", src, {
args = {"Server", "You paid $" .. amount}
})
end)
👉 In this example:
- The client sends an event (
playerPay) to the server. - The server listens for that event, processes it, and sends a response back to the client.
Built-In Events in FiveM
In addition to custom events, FiveM comes with built-in events that you can hook into, such as:
playerConnecting– Fires when a player connects to the server.onResourceStart– Fires when a resource starts.onResourceStop– Fires when a resource stops.playerDropped– Fires when a player leaves the server.
These built-in events are essential for managing your server logic. For example, you can automatically load player data when they connect, or save their progress when they disconnect.
Best Practices for Using Events
To make your scripts efficient and secure, keep these best practices in mind:
- Never trust client input – Always validate data on the server to prevent exploits.
- Use descriptive event names – Avoid confusion by naming events clearly (e.g.,
player:payMoneyinstead ofpay). - Minimize unnecessary events – Don’t spam events every frame; only trigger them when needed.
- Organize your events – Group related events into separate files or resources for easier maintenance.
