M MY Multiplayer: Wiki

Global variables

Built-in Lua global variables available in every MY Multiplayer (MYMP) script and inside event handlers.

MY Multiplayer (MYMP) provides built-in global variables in every Lua script, as well as contextual globals populated while an event handler runs.

In every script

Variable Side Description
root Shared The root element of the element tree. Every element sits below it.
resourceRoot Shared Root element of the resource running the script; elements the resource creates sit below.
localPlayer Client The player element representing this client's local player.
exports Shared Table used to call functions exported by other resources in meta.xml.

Inside an event handler

MYMP sets these globals right before calling the handler and restores their previous values as soon as the handler returns.

Variable Side Description
source Shared The element on which the event was triggered.
this Shared The element the handler was attached to with addEventHandler.
eventName Shared The triggered event's name (string).
client Server The player who sent a remote event, when it arrived from a client through triggerServerEvent.
sourceResource Shared The resource that triggered the event or called the exported function.

Calling exported functions (exports)

exports.resource:function(...) and exports.resource.function(...) behave identically. For the call to succeed:

  1. The function must be declared in an <export> tag of that resource's meta.xml for the side (server, client or shared) on which it is called.
  2. The target resource must be running.

Otherwise, the call logs an error and returns false.

Example

addEvent("onShopBuy", true)

addEventHandler("onShopBuy", resourceRoot, function(itemId)
    -- client: the player who sent the event
    local price = exports.shop:getPrice(itemId)
    outputServerLog(getPlayerName(client) .. " bought " .. itemId .. " for " .. tostring(price))
end)