OnPlayerCommand: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "__NOTOC__ {{Server event}} This event is triggered when a player chats inside the chat box. '''''Note:''' This event triggers regardless of whether the command exists or not. Al...")
 
(Example)
Line 12: Line 12:
==Source==
==Source==
The [[event system#Event source|source]] of this event is the [[player]] who tried to execute a command
The [[event system#Event source|source]] of this event is the [[player]] who tried to execute a command
==Example==
This example prevents players executing more than 5 commands a second, recommended for all servers
<syntaxhighlight lang="lua">
local commandSpam = {}
function preventCommandSpam()
if (not commandSpam[source]) then
commandSpam[source] = 1
-- New person so add to table
elseif (commandSpam[source] == 5) then
cancelEvent()
outputChatBox("Please refrain from command spamming!", source, 255, 0, 0)
-- This person is command spamming!
else
commandSpam[source] = commandSpam[source] + 1
-- Add one to the table
end
end
addEventHandler("onPlayerCommand", root, preventCommandSpam)
setTimer(function() commandSpam = {} end, 1000, 0) -- Clear the table every second
</syntaxhighlight>
{{See also/Server event|Player events}}


==Cancel effect==
==Cancel effect==

Revision as of 14:05, 22 August 2011

This event is triggered when a player chats inside the chat box.

Note: This event triggers regardless of whether the command exists or not. Also, typing anything in chat will execute command "say", so this event will be triggered on every chat message as well.

Parameters

string command

Source

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

Example

This example prevents players executing more than 5 commands a second, recommended for all servers

local commandSpam = {}

function preventCommandSpam()
	if (not commandSpam[source]) then
		commandSpam[source] = 1
		-- New person so add to table
	elseif (commandSpam[source] == 5) then
		cancelEvent()
		outputChatBox("Please refrain from command spamming!", source, 255, 0, 0)
		-- This person is command spamming!
	else
		commandSpam[source] = commandSpam[source] + 1
		-- Add one to the table
	end
end
addEventHandler("onPlayerCommand", root, preventCommandSpam)
setTimer(function() commandSpam = {} end, 1000, 0) -- Clear the table every second

See Also

Player events


Event functions


Cancel effect

The command will not be executed

See Also

Player events


Event functions