# attachElements (Shared)

> Hangs one element on another. From then on it follows the target until it is detached.

- **Platform**: MY Multiplayer (MYMP)
- **Kind**: Lua Function
- **Side**: Shared
- **Category**: Attachments
- **URL**: https://wiki.mymp.gg/en/attachElements

## Syntax

```lua
bool attachElements(element element, element target [, float x, y, z = 0, 0, 0, float rx, ry, rz = 0, 0, 0, string|int socket = nil])
```

## Parameters

| Name | Type | Description |
| --- | --- | --- |
| `element` | `element` | The element to attach. |
| `target` | `element` | The element it hangs on. |
| `x, y, z` | `float` | Offset from the target. *(default: `0, 0, 0`)* |
| `rx, ry, rz` | `float` | Rotation relative to the target. *(default: `0, 0, 0`)* |
| `socket` | `string\|int` | Where on the target to attach: a ped's bone (its MTA id or its rig name) or a vehicle's or object's component or frame. A number only works for peds, players and objects. Without a socket, the element follows the target's root. *(default: `nil`)* |

## Returns

`true` if it was attached. `false` when either element cannot be attached, when it would make a cycle, when they are in different dimensions, when the target has no such socket or when `onElementAttach` was cancelled.

Players, peds, vehicles, objects, sounds, effects and client lights can be attached. Attaching to another target detaches the element from its current one. When the target is destroyed, the element is detached where it stands.

## Example

```lua
local vehicle = createVehicle("infernus", 1544.0, -1675.0, 13.6)
local ped = createPed("male01", 1544.0, -1675.0, 15.0)
attachElements(ped, vehicle, 0, 0, 1.2)
```

### Coming from MTA

Works as in MTA, with the socket as an extra argument.

## 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.
- [setElementAttachedOffsets (Shared)](https://wiki.mymp.gg/en/setElementAttachedOffsets.md): Changes the offset, the rotation and, if asked, the socket of an attached element.
- [getAttachedElements (Shared)](https://wiki.mymp.gg/en/getAttachedElements.md): Lists the elements attached directly to an element.
- [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.
