Vice Underdogs
Scripting => Scripting Support => Topic started by: Inferno69 on October 12, 2020, 03:44:58 am
-
IntroCooldown functions have become a must in any server nowadays in order to prevent any spam/abuse by any player. So, here i would like to post a cooldown function I made for my server and is effective for me.
Requirement:Requires S.L.C Extended Timers : https://forum.vc-mp.org/?topic=1487.msg10339#msg10339 (https://forum.vc-mp.org/?topic=1487.msg10339#msg10339)
OnScriptLoad// timer callback
DecreaseCooldown <- _Timer.Create(this, DecreaseCooldown, 1000, 0);
// db to be created
dbCooldown <- ConnectSQL("Databases/Cooldown.db");
QuerySQL( dbCooldown, "CREATE TABLE IF NOT EXISTS Cooldown ( Cmd TEXT, Name TEXT, Time NUMERIC )" );
Any where in main.nut // Timer
function DecreaseCooldown()
{
QuerySQL( dbCooldown, "UPDATE Cooldown SET Time= (Time - 1) WHERE Time > 0" );
local q = QuerySQL( dbCooldown, "SELECT Cmd FROM Cooldown WHERE Time='0'" );
local cmd = GetSQLColumnData( q , 0);
local name = GetSQLColumnData( q , 1);
if( q && cmd && name )
{
QuerySQL( dbCooldown, "DELETE FROM Cooldown WHERE Cmd='" + cmd + "' AND Name='"+name+"' " );
}
}
// checks if cooldown for a cmd for a player is set or not
function CheckCooldown( command, player )
{
local q = QuerySQL( dbCooldown, "SELECT* FROM Cooldown WHERE Cmd='"+command+"' AND Name='"+player.Name+"' AND Time > 0" );
local atime = GetSQLColumnData( q , 2);
local name = GetSQLColumnData( q, 1 );
local cmda = GetSQLColumnData( q, 0 );
if ( name && atime && cmda ) return 1;
else return 0;
}
// adds cooldown
function AddCooldown( cmd, player, time ) {
if(player) {
QuerySQL( dbCooldown, "INSERT INTO Cooldown ( Cmd, Name, Time) VALUES ( '" + cmd + "', '" + player.Name + "', '"+time+"' )" );
}
}
// checks time remaining for cooldown
function CooldownTime( cmd, player ) {
local q = QuerySQL( dbCooldown, "SELECT* FROM Cooldown WHERE Cmd='"+cmd+"' AND Name='"+player.Name+"' AND Time > 0" );
local atime = GetSQLColumnData( q , 2);
local name = GetSQLColumnData( q, 1 );
local cmda = GetSQLColumnData( q, 0 );
if ( name && atime && cmda ) return atime;
else return 0;
}
Functions to use in commands/any other event// add in command to return if cooldown is set
else if(CheckCooldown(cmd,player)) return MessagePlayer(""+RED+"[Cooldown] You can use this command again after "+CooldownTime(cmd,player)+" seconds ",player);
// add when command can be executed after all checks
AddCooldown( cmd, player, 5 ); // this will add cmd, player and 5 seconds cooldown, these parameters can be edited
ExamplesExample 1// Used in Keybind
function OnKeyDown(player, key) {
if(key == HKEY && player.Vehicle)
{
// by pressing H, it will flip a vehicle and add a cooldown of 2 seconds
if(CheckCooldown(key,player)) return MessagePlayer("[#ffff00][Cooldown] You can use this command again after "+CooldownTime(key,player)+" seconds ",player);
AddCooldown( key, player, 2 );
player.Vehicle.Rotation = Quaternion(0.0, 0.0, player.Vehicle.Rotation.z, player.Vehicle.Rotation.w);
ClientMessage("Flipped.", player, 176, 176, 176);
}
return 1;
}
Example 2
OnPlayerCommand(player, cmd) {
// this cmd will flip the car and add a 2s cooldown
if ( cmd == "flip" )
{
if ( !player.Vehicle ) MessagePlayer( "[#ff1111]Error: [#FF7092]You need to be in a Vehicle to use this command." , player )
else if(CheckCooldown(cmd,player)) return MessagePlayer("[#ffff00][Cooldown] You can use this command again after "+CooldownTime(cmd,player)+" seconds ",player);
else
{
AddCooldown( cmd, player, 2 );
player.Vehicle.Rotation = Quaternion(0.0, 0.0, player.Vehicle.Rotation.z, player.Vehicle.Rotation.w);
ClientMessage("Flipped.", player, 176, 176, 176);
}
return 0;
}
// Command to set manually Cooldown to any value for a player when its existing. Added for Testing Purpose
else if (cmd == "setcool")
{
// add your admin function here
if(!text) return MessagePlayer("[#ffff00]/setcool [player] [cmd/arg] [cooldown]",player);
else {
local plr = GetTok(text, " ", 1);
local acmd = GetTok(text, " ", 2);
local acd = GetTok(text, " ", 3);
local q = QuerySQL( dbCooldown, "SELECT* FROM Cooldown WHERE Cmd='"+acmd+"' AND Name='"+plr+"' AND Time > 0" );
if(!q) return MessagePlayer(""+RED+" That argument is not set to cooldown for that player ",player);
else if(!plr) return MessagePlayer(""+RED+" Please specify a player name ",player);
else if(!acmd) return MessagePlayer(""+RED+" Please specify a command for cd ",player);
else if(!acd) return MessagePlayer(""+RED+" Please specify a cd ",player);
else if(!IsNum(acd)) return MessagePlayer(" "+RED+" CD Values must be in numbers ",player);
else {
QuerySQL(dbCooldown, "UPDATE Cooldown SET Time = '"+acd.tointeger()+"' WHERE Name = '" + plr + "' AND Cmd='"+acmd+"'");
MessagePlayer("[#ffff00] You have set "+acd+" seconds cooldown for "+acmd+" command for player "+plr+" ",player);
}
}
return 0;
}
return 1;
}
FunctionsThis snippet can be used for any event which require Cooldown as explained in Examples
CreditsInferno , S.L.C ( for extended timers ), Xmair ( flip command used in Examples )
Ps : wrong board. Someone kindly move it.
-
Might not be the best idea to invoke a sql query on every event, but overall as starter.
Good Effort!
-
Might not be the best idea to invoke a sql query on every event, but overall as starter.
Good Effort!
Yeah but i havent operated with raw data yet so i had to use what i have . thanks man :)