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

Function

setElementData

Stores a value under a key on an element. By default the value is sent to all players, which fires onElementDataChange.

Syntax

bool setElementData(element theElement, string key, mixed value [, mixed syncMode = true, string clientChangesPolicy = "default"])

Arguments

theElementelement
The element.
keystring
The key.
valuemixed
The value: number, string, table, element, true/false.
syncModemixed, optional = true
true/"broadcast": everyone, false/"local": server only, "subscribe": only players from addElementDataSubscriber.Different default in MTA:SA: string syncMode = "broadcast"
clientChangesPolicystring, optional = "default"
Whether client scripts may change this key: "allow" yes, "deny" no (the change is rejected, the player gets the server value back, the server gets onPlayerChangesProtectedData), "default" = the element_data_client_changes setting in server-config.json ("allow" by default).

Differences from MTA:SA

  • Arguments in a different colour differ from MTA:SA.

In MTA:SA

setElementData(element theElement, string key, var value, string syncMode = "broadcast", string clientChangesPolicy = "default")

Returns

true on success.

Example

addEventHandler("onPlayerLogin", root, function(_, account)
    setElementData(source, "level", getAccountData(account, "level") or 1)
    setElementData(source, "password_hash", "...", false)
end)

Notes

Who changed it. In onElementDataChange after a change from a client the global client is that player; for a server change it is nil.

Only broadcast what clients must know. Every change sent to everyone costs traffic; set server-only data with false.

See also