I've seen a few people still struggling with this, so here is my participation.
This simple commented example script shows you how to avoid command/function spamming.
I wrote this for beginners, so it's heavily commented.
Hope it helps!
Code
PHP Code:
#include <sourcemod>
#pragma semicolon 1
// Define a global array where the times will be stored on the client
new g_iPlayerSpamTime[MAXPLAYERS+1];
// This is kind of a spamcheck spamcheck
new bool:g_bPlayerHadMessage[MAXPLAYERS+1];
// Define the time after which we allow the command again
#define SPAMTIME 5
public OnPluginStart()
{
RegConsoleCmd("sm_test", Command_Test);
}
public Action:Command_Test(client, args)
{
// I do no validity checky here because ths is just an example, but don't forget to do it.
// Check if the client has a stored time, if not let him pass and store it
// I use GetTime here, you can also use GetGameTime/GetEngineTime and fit it to your needs.
if(g_iPlayerSpamTime[client] == 0 || g_iPlayerSpamTime[client] <= ( GetTime() - SPAMTIME ))
{
// Set the time
g_iPlayerSpamTime[client] = GetTime();
// Set the appeared message to false
g_bPlayerHadMessage[client] = false;
// Your function or command
ExampleFunc(client);
}
// If he is not allowed to use the command send him a message
// We only want this message to appear 1 time, else our spamcheck wouldn't make any sense
else if(!g_bPlayerHadMessage[client])
{
// Send a message when he can approximately use the command again.
ReplyToCommand(client, "You are not allowed to use this command in the next %d seconds", SPAMTIME - ( GetTime() - g_iPlayerSpamTime[client] ));
// Set to true that he seen the message
g_bPlayerHadMessage[client] = true;
}
return Plugin_Handled;
}
ExampleFunc(client)
{
ReplyToCommand(client, "Command has run");
}
// Of course don't forget to reset the time if it is a long time
public OnClientPostAdminCheck(client)
{
g_iPlayerSpamTime[client] = 0;
g_bPlayerHadMessage[client] = false;
}
// Of course don't forget to reset the time if it is a long time
public OnClientDisconnect_Post(client)
{
g_iPlayerSpamTime[client] = 0;
g_bPlayerHadMessage[client] = false;
}
Yours sincerely
Impact
__________________