ServersYour serverFor creatorsForumFAQDocumentationSecurity
Download the launcher
  1. Documentation
  2. Shared functions
  3. addEvent

Function

addEvent

Registers a custom event with the given name so it can be triggered (triggerEvent, triggerClientEvent, triggerServerEvent) and handled (addEventHandler). With allowRemoteTrigger = true the event can come from the other side of the network: on the server - from a client via triggerServerEvent, on the client - from the server via triggerClientEvent. Without it, events from the network are rejected, which protects the server from clients faking events.

Syntax

bool addEvent(string eventName [, bool allowRemoteTrigger = false, bool isPrivate = false])

Arguments

eventNamestring
Event name (e.g. "onPlayerBuyCar").
allowRemoteTriggerbool, optional = false
true = it can be triggered from the other side of the network.
isPrivatebool, optional = false
true = private event: triggerEvent works only from the resource that registered it; a call from another resource returns false and prints an error, and another resource cannot register it again.Not in MTA:SA

Differences from MTA:SA

  • Arguments in a different colour differ from MTA:SA.

In MTA:SA

addEvent(string eventName, bool allowRemoteTrigger = false)

Returns

true on success, false when another resource tries to take over a private event.

Example

addEvent("onPlayerBuyCar", true)
addEventHandler("onPlayerBuyCar", root, function(model)
    if client ~= source then return end
    if getPlayerMoney(client) < 10000 then return end
    takePlayerMoney(client, 10000)
    local x, y, z = getElementPosition(client)
    createVehicle(model, x + 3, y, z)
end)

Notes

In a handler of an event raised with triggerEvent, sourceResource and sourceResourceRoot point to the resource that raised it (they are nil for events from the game and from clients). Privacy does not block clients: allowRemoteTrigger decides about them.

On the server always check the client variable (the player who sent it) in a network event handler, not just source, because a client can pass any element as the source.

See also