# detachElements (Shared)

> Detaches an element from the element it hangs on. It stays where it is.

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

## Syntax

```lua
bool detachElements(element element [, element target = nil])
```

## Parameters

| Name | Type | Description |
| --- | --- | --- |
| `element` | `element` | The element to detach. |
| `target` | `element` | If given, the element is only detached when it hangs on this target. *(default: `nil`)* |

## Returns

`true` if it was detached. `false` if the element was not attached, if `target` was given and is not its current target, or if `onElementDetach` was cancelled.

## Example

```lua
addCommandHandler("soltar", function(player)
    local target = getElementAttachedTo(player)
    if target then
        detachElements(player, target)
    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.
- [isElementAttached (Shared)](https://wiki.mymp.gg/en/isElementAttached.md): Tells whether an element is attached to another.
- [getElementAttachedTo (Shared)](https://wiki.mymp.gg/en/getElementAttachedTo.md): Tells which element an element is attached to.
- [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.
