# setElementData (Server)

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

- **Platform**: MY Multiplayer (MYMP)
- **Kind**: Lua Function
- **Side**: Server
- **Category**: Elements
- **URL**: https://wiki.mymp.gg/en/setElementData-server
- **Counterpart**: Also exists on the client, with its own implementation: [setElementData (Client)](https://wiki.mymp.gg/en/setElementData-client.md)

## Syntax

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

## Parameters

| Name | Type | Description |
| --- | --- | --- |
| `element` | `element` | The element. |
| `key` | `string` | The data's name. Keys over 128 characters are cut, with a warning. |
| `value` | `any` | The value to keep. |
| `sync` | `bool\|string` | `true` 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

```lua
addEventHandler("onPlayerJoin", root, function()
    setElementData(source, "money", 500)
    setElementData(source, "admin.notes", "", "local")
end)
```

### Note

A value over 10 MB or over 1048576 values is not synced: the function logs an error and returns `false`.

## See also

- [setElementData (Client)](https://wiki.mymp.gg/en/setElementData-client.md): Keeps a piece of data on an element, on this client only.
- [getElementData (Shared)](https://wiki.mymp.gg/en/getElementData.md): Reads a value stored on an element.
- [addElementDataSubscriber (Server)](https://wiki.mymp.gg/en/addElementDataSubscriber.md): Subscribes a player to an element data key set with the "subscribe" sync mode. The player receives the current value at once and every change after that.
- [onElementDataChange (Server)](https://wiki.mymp.gg/en/onElementDataChange.md): Fired when a piece of an element's data changes or is removed. Cancelling the event puts the previous value back (or removes the key if it did not exist), and the change is not sent to the players.
