onPlayerCommand | Multi Theft Auto: Wiki Skip to content

onPlayerCommand

Client-side
Server-side
Shared

This event is triggered when a player issues a command.

This event triggers regardless of whether the command exists in a script or is hardcoded. Also, typing anything in chat will execute the internal command "say", so this event will be triggered on every chat message as well. Therefore you should avoid excessive use of this function on busy servers, out of performance considerations.

Parameters

string command
  • command: a string containing the name of the command executed.

Source

element: The source of this event is the player who tried to execute a command.

Canceling

The command will not be executed. (Only server-side commands can be canceled.)

Code Examples

server

This example implements an anti-flood protection timer for commands.Be sure to give the resource a function.kickPlayer right.Also, note, that if the server freezes for a frame and a player executes a command 2x he/she'll get kicked, because those 2 commands will be processed after each other, with very little delay

local CMD_INTERVAL = 200 --// The interval that needs to pass before the player can execute another cmd
local KICK_AFTER_INTERVAL = 50 --// Kick the player if they execute more than 20 cmds/sec
local executions = setmetatable({}, { --// Metatable for non-existing indexes
__index = function(t, player)
return getTickCount()-CMD_INTERVAL
end
})
addEventHandler("onPlayerCommand", root,
function()
if (executions[source]-getTickCount()<=CMD_INTERVAL) then
if (executions[source]-getTickCount()<=KICK_AFTER_INTERVAL) then
kickPlayer(source, "Anti-Flood", "Don't flood")
end
cancelEvent()
end
executions[source] = getTickCount()
end
)

See Also