- Documentation
- Shared functions
- 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 = falsetrue= it can be triggered from the other side of the network.isPrivatebool, optional = falsetrue= private event:triggerEventworks only from the resource that registered it; a call from another resource returnsfalseand 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)
-- zdarzenie tylko dla tego zasobu: inne zasoby nie wyplaca sobie pieniedzy
addEvent("bank:payout", false, true)
addEventHandler("bank:payout", resourceRoot, function(player, amount)
givePlayerMoney(player, amount)
end)
triggerEvent("bank:payout", resourceRoot, getRandomPlayer(), 100)
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
addEventHandlerAttaches a function to an event. This is how you react to a player joining, dying, a key press, every frame and your own events.triggerServerEventSends an event from the player to the server, for example a button click or a menu choice.triggerClientEventSends an event from the server to one player, several players or everyone at once.isEventAddedChecks whether an event with this name exists (built in or added withaddEvent).