addEventHandler | Multi Theft Auto: Wiki Skip to content

addEventHandler

Client-side
Server-side
Shared

This page is incomplete! Help wanted!

Please finish this page using the corresponding Old Wiki article.
Go to Contribution guidelines for more information.


This function will add an event handler. An event handler is a function that will be called when the event it's attached to is triggered. See event system for more information on how the event system works.

Note

You shouldn't re-use the same name for your handler function as the event name - if multiple handler functions are used, as this can lead to confusion. On the same note, for multiple reasons, it isn't a good idea to export the same functions that you use locally as remote event handlers.

Note

See Event Source Element for a descriptive visualization of the event system handling an event trigger.

Important

See Script security for how-to prevent cheaters from abusing event system and element data .

Important

Anything bound to a specific element will be run before other handlers that are bound to something higher in the element tree (like root) This means that "high+10" bound to root won't trigger before "normal" bound directly to an element.

Note

Due to the additional set of global variables, the event-trigger specific variables it is NOT a good idea to use the same function locally as well as directly as an event handler . Event handlers often make use of the source element variable which would often find no use in generic functions. Inside of server-side remote event handlers it is important to add protections against exploits due to unexpected client event triggers or network-based load situations while generic functions, due to being part of a controlled call-stack, do not in general face the same issues. It is recommended to adapt a good-natured distancing principle between code meant to run from local logic in separation to code meant to run from remote logic.

Syntax

addEventHandler ( )

Code Examples

server

This serverside example sends a message to everyone in the server when a player spawns.

-- define our handler function
function onPlayerSpawnHandler ( )
-- get the player's name, source is the player because he was spawned
local playerName = getPlayerName( source )
-- output in the chat box that they've spawned
outputChatBox ( playerName .. " has spawned!" )
end
addEventHandler( "onPlayerSpawn", root, onPlayerSpawnHandler ) -- root is a predefined global variable for getRootElement()