- Documentation
- Shared functions
- 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 = truetrue/"broadcast": everyone,false/"local": server only,"subscribe": only players fromaddElementDataSubscriber.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 getsonPlayerChangesProtectedData),"default"= theelement_data_client_changessetting 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
getElementDataReturns an element's data value under a key. Data is set withsetElementData; broadcast data is visible on the client too.removeElementDataRemoves an element data value under a key (for all players).onElementDataChangeAn element's data changed (setElementDataorremoveElementData), also from a client.