- Documentation
- Shared functions
- addEventHandler
Function
addEventHandler
Attaches a function to an event. This is how you react to a player joining, dying, a key press, every frame and your own events.
Syntax
bool addEventHandler(string name, element attachedTo, function fn [, bool propagate = true, string priority = "normal"])Arguments
namestring- Event name, e.g.
onPlayerJoinor your own fromaddEvent. attachedToelement- The element whose events you catch.
rootcatches all,resourceRootonly this resource's elements. fnfunction- The handler.
sourceandeventNameare available inside. propagatebool, optional = true- Whether to also catch events of elements lower in the tree.
prioritystring, optional = normalhigh,normalorlow. Decides the order when several functions handle the same event.
Returns
true if the function was attached, or false if the same function is already attached to this event and element.
Example
local function greet()
outputChatBox("Hi, " .. getPlayerName(source) .. "!", source)
end
addEventHandler("onPlayerJoin", root, greet)
-- the same function a second time: returns false
addEventHandler("onPlayerJoin", root, greet)
Notes
Good to know. Detach the function with removeEventHandler and the same arguments. You can stop an event with cancelEvent() if the event's description allows it.
See also
onPlayerJoinA player joined the server. A good place for a greeting, login and the first spawn.onClientRenderTriggered every game frame. This is where you draw everything that should stay on screen: HUD, counters, text.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.removeEventHandlerDetaches a function from an event. You must pass the same name, element and the same function (the same variable) as inaddEventHandler- an anonymous function written again is a different function.