M MY Multiplayer: Wiki
Side
Kind

addEventHandler

Shared

Makes a function run whenever the event happens on the given element or, with propagate, on any of its descendants.

bool addEventHandler(string eventName, element attachedTo, function handler [, bool propagate = true, string priority = "normal"])

Parameters

NameTypeDescription
eventNamestringThe name of a registered event (built in or made with addEvent).
attachedToelementThe element the handler listens on. Use root to hear the event on any element.
handlerfunctionThe function called with the event's arguments.
propagateboolAlso run when the event happens on a descendant of attachedTo. With false, only when the source is attachedTo itself.default true
prioritystring"high", "normal" or "low". Higher priority handlers run first; equal ones in the order they were added.default "normal"

Returns

true if it was added, false if the event does not exist or that function is already on that event of that element.

Inside the handler, the globals source (where the event happened), this (the element the handler is attached to), eventName, client (on the server, the player who triggered a remote event) and sourceResource tell where the event came from. A resource's handlers are removed when it stops.

Example

addEventHandler("onPlayerJoin", root, function()
    outputDebugString(getPlayerName(source) .. " joined")
end, true, "high")
Coming from MTAOnly the names high, normal and low count. An adjustment such as "high+5" is worth the same as "normal".

See also