# onElementAttach (Server)

> Fired when the server calls `attachElements`, before the element is attached. Cancelling the event stops the attachment and makes `attachElements` return `false`.

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

## Syntax

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

## Handler parameters

| Name | Type | Description |
| --- | --- | --- |
| `attachedTo` | `element` | The element it will hang on. |
| `x, y, z` | `float` | The requested offset from the target. |
| `rx, ry, rz` | `float` | The requested rotation relative to the target. |

## Source

The element about to be attached.

## Example

```lua
addEventHandler("onElementAttach", root, function(attachedTo)
    if getElementType(source) == "player" and getElementType(attachedTo) == "vehicle" then
        cancelEvent()
    end
end)
```

## See also

- [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.
- [onElementDetach (Server)](https://wiki.mymp.gg/en/onElementDetach.md): 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.
- [getElementAttachedOffsets (Shared)](https://wiki.mymp.gg/en/getElementAttachedOffsets.md): Reads the offset, rotation and socket an element is attached with.
