M MY Multiplayer: Wiki
Side
Kind

setElementData

Server

Keeps a piece of data on an element and, depending on sync, sends it to the players.

Also exists on the client, with its own implementation: setElementData

bool setElementData(element element, string key, any value [, bool|string sync = true])

Parameters

NameTypeDescription
elementelementThe element.
keystringThe data's name. Keys over 128 characters are cut, with a warning.
valueanyThe value to keep.
syncbool|stringtrue or "broadcast" sends it to every player; false or "local" keeps it on the server; "subscribe" sends it only to the players added with addElementDataSubscriber. Any other value raises an error.default true

Returns

true if it was kept, false if the element already held that value with that sync, a handler cancelled the change or the value is too big to sync.

Fires onElementDataChange. Cancelling the event puts the previous value back. Switching existing data to "subscribe" keeps its subscribers.

Example

addEventHandler("onPlayerJoin", root, function()
    setElementData(source, "money", 500)
    setElementData(source, "admin.notes", "", "local")
end)
NoteA value over 10 MB or over 1048576 values is not synced: the function logs an error and returns false.

See also