setElementData | Multi Theft Auto: Wiki Skip to content

setElementData

Client-side
Server-side
Shared

Pair: getElementData

Updated in 1.6.0-9.22815

This function stores element data under a certain key, attached to an element. Element data set using this is then synced with all clients and the server (by default).

Tip
  • A simple and efficient way to make a variable known to the server and clients is to use setElementData on the root element.
  • See element data perfromance for tips on optimizing the use of element data.
Note
  • The data can contain server-created elements, but you should avoid passing data that is not able to be synced such as xmlnodes, acls, aclgroups etc.
  • A subscription mode has been introduced for setElementData serverside. When setting data in subscription mode, only clients that are added through addElementDataSubscriber will receive the data, which is good for performance. Note this mode only works when setting element data serverside. Setting data clientside still sends the update to all clients if 'synchronize' is set to true.
Caution

For performance reasons, never use setElementData (with sync = true) in events that fire often (like onClientRender) without further optimization or conditions. In fact, using element data in general, can take such a toll on performance that not using it unless strictly necessary (e.g use alternatives such as storing data in tables) is recommended.

Important
  • As element data is synced to all clients (by default), it can generate a lot of network traffic and be heavy on performance. Events are much more efficient for sending data from a client to the server only, or from the server to a specific client. Usage of element data should be discouraged where your goal can be achieved with events, and tables for storing and retrieving data. You can also use the subscription mode on serverside.
  • See Element data security for tips on preventing cheaters when using events and element data.

OOP Syntax Help! I don't understand this!

Client Syntax

bool setElementData ( ​element theElement, ​string key, ​var value, [ ​bool synchronize = true ] )
Required Arguments
  • theElement: The element you wish to attach the data to.
  • key: The key you wish to store the data under. (Maximum 128 characters.)
  • value: The value you wish to store. See element data restrictions.
Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use.

  • synchronize (default: true): Determines whether or not the data will be synchronized with the server.

Returns

  • bool: result

Returns true if the data was set successfully, false otherwise.

Server Syntax

bool setElementData ( ​element theElement, ​string key, ​var value, [ ​string syncMode = "broadcast", ​string clientChangesPolicy = "default" ] )
Required Arguments
  • theElement: The element you wish to attach the data to.
  • key: The key you wish to store the data under. (Maximum 128 characters.)
  • value: The value you wish to store. See element data restrictions.
Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use.

  • syncMode (default: "broadcast"): Synchronization mode.
    • broadcast: Synchronize to all clients (default behavior). You can also parse true for this option.
    • local: Don't synchronize. You can also parse false for this option.
    • subscribe: Only synchronize to specific clients. See addElementDataSubscriber and removeElementDataSubscriber.
  • clientChangesPolicy (default: "default"): Client changes policy.
    • default: Use elementdata_whitelisted setting from mtaserver.conf.
    • allow: Trust changes from clients.
    • deny: Deny client changes. The server will trigger the onPlayerChangesProtectedData event when the client attempts to change the value.

Returns

  • bool: result

Returns true if the data was set successfully, false otherwise.

Code Examples

client

This example gives the player a score for each kill they make.

addEventHandler("onClientPlayerWasted", root, function(killer, weapon)
if (killer == localPlayer) then
local scoreToAdd = weapon == 0 and 10 or 5 -- Fist kills give more points
local currentScore = getElementData(localPlayer, "playerScore") or 0
setElementData(localPlayer, "playerScore", currentScore + scoreToAdd, false) -- Data not synchronized to server
end
end)
addCommandHandler("showscore", root, function()
local score = getElementData(localPlayer, "playerScore") or 0
outputChatBox("Your current score is: " .. score)
end)
addEventHandler('onClientResourceStart', resourceRoot, function()
setElementData(localPlayer, "playerScore", 0, false) -- Data not synchronized to server
end)

Changelog

  • 1.5.8-9.20477

    Added syncMode argument.

  • 1.6.0-9.22815

    Added clientChangesPolicy argument.

  • See Also

    Element Functions