- Documentation
- Shared functions
- 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 = falsetrue=/Carand/carare 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
removeCommandHandlerRemoves a command added withaddCommandHandler. With a function it removes only that handler; without one it removes every handler of the command.executeCommandHandlerRuns a command registered withaddCommandHandleras if a player typed it. On the server you pass a player (ornil) and the arguments: a single string is split into words like in chat. On the client it runs client commands.getCommandHandlersReturns the names of commands registered by a resource, or by every resource without an argument.bindKeyBinds a function or a command to a player's key. The function gets (on the server also the player) the key name, the state and the extra arguments frombindKey. Instead of a function you can pass a command name: the key runs it like/command. On the server the player's game watches the key and reports presses to the server.