Events
Events are named messages. Any resource can fire an event, and every resource that listens to it runs its handler.
Local events
Section titled “Local events”TriggerEvent reaches all resources on the same side (server or client). It never travels over the network.
-- resource AAddEventHandler('race:finished', function(playerId, timeMs) print(('Player %d finished in %d ms'):format(playerId, timeMs))end)
-- resource B, same sideTriggerEvent('race:finished', 12, 83450)A handler can call CancelEvent() to tell the code that fired the event to skip its default action. That code checks WasEventCanceled() right after TriggerEvent. Other handlers still run. The chat uses this for chatMessage.
-- resource BTriggerEvent('shop:beforeBuy', source, item)if WasEventCanceled() then return endNetwork events
Section titled “Network events”To receive an event from the other side, register it with RegisterNetEvent. Without that call, network events never reach your handlers.
TriggerServerEvent('myshop:buy', 'water')RegisterNetEvent('myshop:buy', function(itemId) local player = source -- who sent it -- check and act, see the rules belowend)The server answers one player, or everyone with -1:
TriggerClientEvent('myshop:bought', player, 'water')TriggerClientEvent('chat:announce', -1, 'Shop closes in 5 minutes')Security rules for network events
Section titled “Security rules for network events”Players can send any event with any arguments. A server handler must assume the worst.
- Check every argument. Type, range and length.
if type(itemId) ~= 'string' or #itemId > 32 then return end - Never trust amounts or prices from the client. Look them up on the server.
- Check permissions with
source, not with a player ID sent as an argument. - Copy
sourceinto a local before callingWait, because another event can change it. - Rate-limit expensive actions per player.
Naming
Section titled “Naming”Prefix events with your resource name: myshop:buy, not buy. Names are shared by all resources.
Arguments
Section titled “Arguments”Arguments are copied: tables, strings, numbers, booleans and nil work. In local events (TriggerEvent), functions are passed as references, so callbacks like TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end) work; see Exports. Network events cannot carry functions: they arrive as nil.
Removing handlers
Section titled “Removing handlers”local handle = AddEventHandler('race:tick', onTick)-- laterRemoveEventHandler(handle)See the reference: Events, Built-in events.