# addEvent (Shared)

> Registers a new event under this name. Only then can handlers be added to it and can it be triggered.

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

## Syntax

```lua
bool addEvent(string name [, bool allowRemoteTrigger = false])
```

## Parameters

| Name | Type | Description |
| --- | --- | --- |
| `name` | `string` | The event's name. |
| `allowRemoteTrigger` | `bool` | Let the other side trigger the event with `triggerServerEvent`, `triggerClientEvent` or their latent versions. *(default: `false`)* |

## Returns

`true` if it was registered, `false` if the name is empty or an event with that name already exists.

The event stops existing when the resource that registered it stops.

## Example

```lua
addEvent("onPlayerBuyItem", true)

addEventHandler("onPlayerBuyItem", root, function(itemId)
    outputDebugString(getPlayerName(client) .. " bought item " .. tostring(itemId))
end)
```

### Note

An event that arrives from the other side without `allowRemoteTrigger` is dropped, with only a line in the log.

## See also

- [addEventHandler (Shared)](https://wiki.mymp.gg/en/addEventHandler.md): Makes a function run whenever the event happens on the given element or, with propagate, on any of its descendants.
- [triggerEvent (Shared)](https://wiki.mymp.gg/en/triggerEvent.md): Triggers an event on this side, on source. The element's own handlers and its ancestors' propagating ones are called right away, before the function returns.
- [triggerServerEvent (Client)](https://wiki.mymp.gg/en/triggerServerEvent.md): Sends an event to the server, which triggers it on source there. The function returns before the server receives it.
- [triggerClientEvent (Server)](https://wiki.mymp.gg/en/triggerClientEvent.md): Sends an event to the players' clients, which trigger it on source. The function returns before the clients receive it.
