How to create cooldown for a command?

Started by Kelvinvenema, Dec 25, 2019, 01:22 PM

Previous topic - Next topic

Kelvinvenema

Hello guys can anyone help me how to add cooldowns to commands?

Maximiliano

You can create a function and then you use the NewTimer function:
NewTimer (Function, Milliseconds, Number of times to be repeated, function parameters);

Example:
function Test( player )
{
    MessagePlayer("Timer working correctly", player);
}

NewTimer("Test", 1000, 2, player);
1000 miliseconds = 1 second. 2 = Number of times to be repeated. - player = function parameters


Another example:
function GotoPlayer( player, player2 )
{
    player.Pos = player2.Pos;
    MessagePlayer("You has teleported to " + player2.Name + ".", player);
}

NewTimer("GotoPlayer", 3000, 1, player, player2);
3000 miliseconds = 3 second. 1 = Number of times to be repeated. - player,  player2 = function parameters.

Kelvinvenema

Quote from: Maximiliano on Dec 25, 2019, 02:07 PMYou can create a function and then you use the NewTimer function:
NewTimer (Function, Milliseconds, Number of times to be repeated, function parameters);

Example:
function Test( player )
{
    MessagePlayer("Timer working correctly", player);
}

NewTimer("Test", 1000, 2, player);
1000 miliseconds = 1 second. 2 = Number of times to be repeated. - player = function parameters


Another example:
function GotoPlayer( player, player2 )
{
    player.Pos = player2.Pos;
    MessagePlayer("You has teleported to " + player2.Name + ".", player);
}

NewTimer("GotoPlayer", 3000, 1, player, player2);
3000 miliseconds = 3 second. 1 = Number of times to be repeated. - player,  player2 = function parameters.

i don't know that that works in my example
for example i will use
a command like fixvehicle
how do i add a cooldown so that players can only use this command every 10m
your example doesn't give me the lines i can use.

Maximiliano

#3
Quote from: Kelvinvenema on Dec 25, 2019, 02:14 PM
Quote from: Maximiliano on Dec 25, 2019, 02:07 PMYou can create a function and then you use the NewTimer function:
NewTimer (Function, Milliseconds, Number of times to be repeated, function parameters);

Example:
function Test( player )
{
    MessagePlayer("Timer working correctly", player);
}

NewTimer("Test", 1000, 2, player);
1000 miliseconds = 1 second. 2 = Number of times to be repeated. - player = function parameters


Another example:
function GotoPlayer( player, player2 )
{
    player.Pos = player2.Pos;
    MessagePlayer("You has teleported to " + player2.Name + ".", player);
}

NewTimer("GotoPlayer", 3000, 1, player, player2);
3000 miliseconds = 3 second. 1 = Number of times to be repeated. - player,  player2 = function parameters.

i don't know that that works in my example
for example i will use
a command like fixvehicle
how do i add a cooldown so that players can only use this command every 10m
your example doesn't give me the lines i can use.



Use an array to save the commands that the user can no longer use.
Example:
CommandsUsed <- array( GetMaxPlayers(), []);

function onPlayerCommand( player, cmd, text )
{
if ( cmd.tolower() == "fix" )
{
if ( CommandsUsed[ player.ID ].find("fix") ) MessagePlayer("You can't use this command in this time. (Wait approximately less than 10 minutes) ",player);
else
{
//Your code here...
CommandsUsed[ player.ID ].push( "fix" );
NewTimer("CommandRelease", 600000, 1, player, cmd);

}
}
}


function CommandRelease(player, cmd) //This will eliminate the array command, so the player can use it again
{
if ( CommandsUsed[ player.ID ].find( cmd ))
{
local position = CommandsUsed[ player.ID ].find( cmd );
CommandsUsed[ player.ID ].remove(position);
}
}

It's untested XD

DraGone

#4
I'd say you should avoid timer for that and use table to store such information. E.g. (untested):
/* Global table that will be used to store commands' cooldown for each player */
_cmdCoolDown <- { };


function onPlayerJoin( player ) {
/* Add the player to the cooldown table */
// In this example i use player's name to identify the player but you can also use player's ID or even UID
_cmdCoolDown.rawset( player.Name, { } );
}


function onPlayerPart( player, reason ) {
/* Remove the player from cooldown table */
if ( _cmdCoolDown.rawin( player.Name ) )
{
_cmdCoolDown.rawdelete( player.Name );
}
}


function onPlayerCommand( player, cmd, text )
{
if ( cmd == "fixveh" )
{
// time() will return the unix time
// which is the number of seconds that have elapsed since the time 00:00:00 UTC on 1 January 1970
local unix_time_now = time( );

/* Check whether the cooldown time has passed or not */
// if cooldown time value is higher than unix_time_now, that means it's not passed yet
if ( _cmdCoolDown[player.Name].rawin("FixVeh") && _cmdCoolDown[player.Name]["FixVeh"] > unix_time_now )
{
/* If it's not passed, inform player */
local time_left = ( unix_time_now - _cmdCoolDown[player.Name]["FixVeh"] );
PrivMessage( player, "Please wait "+ time_left +" more seconds to use this command.." );
}
else
{
// Fix veh..etc
//

/* If everything is okay, set new cooldown time */
_cmdCoolDown[player.Name].rawset( "FixVeh", (unix_time_now + 600) ); // 600 sec = 10 mins
}
}
}

D4rkR420R

#5
Here's a more compact way of adding cooldowns to systems. Be advised that it's untested, however in theory, it should do the trick.
fixTimer <- array( 1000, 0 ); // For vehicles. If you want one for players, use array( GetMaxPlayers(), 0 );
fixTimerValue <- 600; // 10 minutes after usage

function onPlayerCommand( player, cmd, text )
{
    switch( cmd.tolower() )
    {
        case "fix":
            if( fixTimer[ vehicle.ID ] > time() ) return MessagePlayer( "Error: You need to wait 10 minutes to repair your vehicle.", player );
            local veh = player.Vehicle;
            if( veh )
            {
                veh.Health = 1000;
                fixTimer[ vehicle.ID ] = time() + fixTimerValue;
                MessagePlayer( "Successfully repaired your vehicle.", player );
            }
        break;
    }
}

Kelvinvenema