ServersYour serverFor creatorsForumFAQDocumentationSecurity
Download the launcher
  1. Documentation
  2. Shared functions
  3. addCommandHandler

Function

addCommandHandler

Registers a chat and console command (e.g. /car). When a player types it, the function is called. On the server the function gets the player, the command name and the following words as separate arguments; on the client the command name and the words. One command can have several functions, also from different resources. The command goes away with the resource that added it.

Syntax

Server

bool addCommandHandler(string commandName, function handler [, bool restricted = false, bool caseSensitive = false])

Client

bool addCommandHandler(string commandName, function handler)

Arguments

commandNamestring
Command name without the slash.Not in MTA:SA
handlerfunction
Server: function(player, commandName, ...), client: function(commandName, ...).Not in MTA:SA
restrictedbool, optional = false
Accepted for compatibility with existing scripts. Check permissions inside the function, e.g. with hasObjectPermissionTo.Not in MTA:SA
caseSensitivebool, optional = false
true = /Car and /car are different commands (server only).Not in MTA:SA

Differences from MTA:SA

  • Arguments in a different colour differ from MTA:SA.

In MTA:SA

addCommandHandler()

Returns

true if the command was added.

Example

The /hp 50 command sets the player's health; the number arrives as text.

addCommandHandler("hp", function(player, command, value)
    local hp = tonumber(value)
    if not hp then
        outputChatBox("Użycie: /" .. command .. " <0-100>", player, 255, 80, 80)
        return
    end
    setElementHealth(player, math.max(0, math.min(100, hp)))
end)

See also