# addCommandHandler (Shared)

> Registers a function that runs when the command is typed in the console. The arguments arrive as space-separated strings.

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

## Syntax

```lua
bool addCommandHandler(string commandName, function handlerFunction)
```

## Parameters

| Name | Type | Description |
| --- | --- | --- |
| `commandName` | `string` | The command's name, without a slash. Case matters. |
| `handlerFunction` | `function` | The function called. On the server it receives `(player, command, ...)`, where the player is the console when the command comes from the server console. On the client it receives `(command, ...)`. |

## Returns

`true` if it was registered. `false` if the name is empty or this resource already has a handler for that command.

Several resources can handle the same command, and all of them run. On the client, a command no client script handles is sent to the server, where `onPlayerCommand` fires before the handlers. A resource's handlers go away when it stops.

## Example

```lua
addCommandHandler("vida", function(player, command, amount)
    local health = tonumber(amount)
    if health then
        setElementHealth(player, health)
    end
end)
```

## See also

- [removeCommandHandler (Shared)](https://wiki.mymp.gg/en/removeCommandHandler.md): Removes the handler this resource registered for a command.
- [executeCommandHandler (Server)](https://wiki.mymp.gg/en/executeCommandHandler-server.md): Runs the handlers server scripts registered for a command, as if the player had typed it.
- [bindKey (Shared)](https://wiki.mymp.gg/en/bindKey.md): Binds a key to a function or a command. The function runs when the key is pressed, released or both.
- [onPlayerCommand (Server)](https://wiki.mymp.gg/en/onPlayerCommand.md): Fired when a player sends a command to the server, before it runs. Cancelling the event stops the command.
