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

Function

addEventHandler

Server and clientEventsIdentical to MTA:SA

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. onPlayerJoin or your own from addEvent.
attachedToelement
The element whose events you catch. root catches all, resourceRoot only this resource's elements.
fnfunction
The handler. source and eventName are available inside.
propagatebool, optional = true
Whether to also catch events of elements lower in the tree.
prioritystring, optional = normal
high, normal or low. 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