# onElementDetach (Server)

> Fired before an element is detached on the server, by `detachElements` or by `attachElements` moving it to another target. Cancelling the event keeps the element attached.

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

## Syntax

```lua
addEventHandler("onElementDetach", root, function(element detachedFrom, float x, y, z, float rx, ry, rz) ... end)
```

## Handler parameters

| Name | Type | Description |
| --- | --- | --- |
| `detachedFrom` | `element` | The element it hangs on. |
| `x, y, z` | `float` | The element's world position at that moment. |
| `rx, ry, rz` | `float` | The element's rotation at that moment. |

## Source

The element about to be detached.

When the target is destroyed, the elements hanging on it are detached without firing this event. If the event is cancelled during an `attachElements` to another target, that `attachElements` returns `false`.

## Example

```lua
addEventHandler("onElementDetach", root, function(detachedFrom)
    if getElementType(source) == "object" and getElementType(detachedFrom) == "vehicle" then
        cancelEvent()
    end
end)
```

## See also

- [detachElements (Shared)](https://wiki.mymp.gg/en/detachElements.md): Detaches an element from the element it hangs on. It stays where it is.
- [onElementAttach (Server)](https://wiki.mymp.gg/en/onElementAttach.md): Fired when the server calls attachElements, before the element is attached. Cancelling the event stops the attachment and makes attachElements return false.
- [attachElements (Shared)](https://wiki.mymp.gg/en/attachElements.md): Hangs one element on another. From then on it follows the target until it is detached.
