# onElementDataChange (Server)

> 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.

- **Platform**: MY Multiplayer (MYMP)
- **Kind**: Lua Event
- **Side**: Server
- **Category**: Elements
- **Cancellable**: `cancelEvent`
- **URL**: https://wiki.mymp.gg/en/onElementDataChange

## Syntax

```lua
addEventHandler("onElementDataChange", root, function(string key, any oldValue, any newValue) ... end)
```

## Handler parameters

| Name | Type | Description |
| --- | --- | --- |
| `key` | `string` | The data's name. |
| `oldValue` | `any` | The previous value, or `nil` if the key did not exist. |
| `newValue` | `any` | The new value, or `nil` if the data was removed. |

## Source

The element whose data changed.

A client's data never reaches the server, so the change always comes from a server script.

## Example

```lua
addEventHandler("onElementDataChange", root, function(key, oldValue, newValue)
    if key == "admin" and getElementType(source) == "player" then
        cancelEvent()
    end
end)
```

## See also

- [setElementData (Server)](https://wiki.mymp.gg/en/setElementData-server.md): Keeps a piece of data on an element and, depending on sync, sends it to the players.
- [getElementData (Shared)](https://wiki.mymp.gg/en/getElementData.md): Reads a value stored on an element.
- [removeElementData (Shared)](https://wiki.mymp.gg/en/removeElementData.md): Deletes a piece of data kept on an element.
