Skip to content
GAMPEONCreate
New project
Preview: this works today, but details can still change.

Events

Resources talk to each other and between server and client through named events.

AddEventHandler(eventName, handler) --> EventHandle

Run a function whenever an event fires. Server and client.

Handlers run for local events from TriggerEvent and, once the event is registered with RegisterNetEvent, for events from the other side. Each handler runs in its own thread, so it may call Wait.

Parameter Type Description
eventName string Name of the event, for example “myshop:buy”. Prefix it with your resource name to avoid clashes.
handler fun(...: any) Called with the arguments of the event.

Returns

  • EventHandle: Pass it to RemoveEventHandler to remove the handler again.
AddEventHandler('myshop:opened', function(shopId)
print('Shop opened: ' .. shopId)
end)

Compared to FiveM: Same as in FiveM.

RemoveEventHandler(handle)

Remove a handler added with AddEventHandler. Server and client.

Parameter Type Description
handle EventHandle Value returned by AddEventHandler or RegisterNetEvent.
local h = AddEventHandler('tick', function() end)
RemoveEventHandler(h)

Compared to FiveM: Same as in FiveM.

RegisterNetEvent(eventName, handler?) --> EventHandle?

Allow an event to arrive from the other side (client to server or server to client). Server and client.

Without this call, events from the network never reach your handlers. On the server, treat every network event as untrusted input: check source and every argument, because players can send any values.

Parameter Type Description
eventName string Name of the event.
handler (optional) fun(...: any) Optional handler, same as calling AddEventHandler afterwards.

Returns

  • EventHandle?: The handle when a handler was given, otherwise nothing.
RegisterNetEvent('myshop:buy', function(itemId)
local player = source
if type(itemId) ~= 'string' then return end
-- check that the player may buy the item, then give it
end)

Compared to FiveM: Same as in FiveM.

RegisterServerEvent(eventName, handler?) --> EventHandle?

Old name of RegisterNetEvent, kept for ported FiveM scripts. Server and client.

Parameter Type Description
eventName string Name of the event.
handler (optional) fun(...: any) Optional handler.

Returns

  • EventHandle?: The handle when a handler was given.

Compared to FiveM: Same as in FiveM.

TriggerEvent(eventName, ...)

Fire an event for all resources on the same side. Server and client.

Handlers of every running resource on this side receive the event. It never leaves the machine.

Parameter Type Description
eventName string Name of the event.
... any Arguments. Values are copied: tables, strings, numbers, booleans and nil. Functions arrive as callable functions that run in the resource that passed them.
TriggerEvent('myshop:opened', 'downtown')
-- callback pattern from ESX
TriggerEvent('myframework:getSharedObject', function(obj) Framework = obj end)

Compared to FiveM: Same as in FiveM, including functions as arguments.

CancelEvent()

Cancel the event whose handler is running. Server and client.

Other handlers still run. The code that fired the event checks WasEventCanceled right after TriggerEvent and can skip its default action. Only works before the handler calls Wait.

AddEventHandler('chatMessage', function(source, name, message)
if message:find('badword') then
CancelEvent()
end
end)

Compared to FiveM: Same as in FiveM.

WasEventCanceled() --> boolean

Check if a handler canceled the last event fired with TriggerEvent. Server and client.

Returns

  • boolean: true if a handler called CancelEvent.
TriggerEvent('myshop:beforeBuy', source, item)
if WasEventCanceled() then
return
end

Compared to FiveM: Same as in FiveM.

TriggerServerEvent(eventName, ...)

Send an event from the client to the server. Client only.

The server only receives it if a resource registered it with RegisterNetEvent. The server sees the sending player in source.

Parameter Type Description
eventName string Name of the event.
... any Arguments, copied like in TriggerEvent. Functions do not travel over the network and arrive as nil.
TriggerServerEvent('myshop:buy', 'water')

Compared to FiveM: Same as in FiveM.

TriggerClientEvent(eventName, target, ...)

Send an event from the server to one player or to everyone. Server only.

Parameter Type Description
eventName string Name of the event.
target integer Player ID, or -1 for all players.
... any Arguments, copied like in TriggerEvent. Functions do not travel over the network and arrive as nil.
TriggerClientEvent('myshop:bought', source, 'water')
TriggerClientEvent('chat:announce', -1, 'Server restart in 5 minutes')

Compared to FiveM: Same as in FiveM.

Report a problem with this pageOther ways to give feedback