#1

I'm trying to place a cooldown timer on this command, is anyone able to help?

Код:
CMD:cupgrade(playerid, params[])
{
	new string[1024];
	if(PlayerInfo[playerid][pCPlan] == 1)
	{
		SendClientMessageEx(playerid, COLOR_GRAD2, "  You have already purchased this upgrade!");
		return 1;
	}
	if(PlayerInfo[playerid][pCPlan] == 0)
	{
		if(GetPlayerCash(playerid) < 1499)
		{
        	SendClientMessageEx(playerid, COLOR_GRAD2, "  You must have enough money (1500$) to purchase this upgrade!");
			return 1;
		}
		else if(PlayerInfo[playerid][pPnumber] >= 1)
		{
			SendClientMessageEx(playerid, COLOR_YELLOW, "SMS: Thank you for upgrading!, Sender: Phones&U");
			RingTone[playerid] = 20;
			PlayerInfo[playerid][pCPlan] += 1;
			new randphone = 1000 + random(9999);//minimum 1000  max 9999
			ReplacePH(PlayerInfo[playerid][pPnumber], randphone);
			PlayerInfo[playerid][pPnumber] = randphone;
			format(string, sizeof(string), "Your number has been changed, your new phone number is %d.", randphone);
			SendClientMessageEx(playerid, COLOR_GRAD4, string);
			SetPVarInt(playerid, "CellCooledDown", 1);
			SetTimerEx("CellCooldown", 3600000, 0, "i", playerid);
			GivePlayerCash(playerid, -1500);
		}
		else
		{
			SendClientMessageEx(playerid, COLOR_GRAD2, "  You need to purchase a phone before upgrading!");
			return 1;
		}
	}
	return 1;
}
Код:
forward CellCooldown(playerid);
public CellCooldown(playerid)
{
	SetPVarInt(playerid, "CellCooledDown", 1);
	return 1;
}
Reply
#2

Timers should never be used for command cooldowns. Use a timestamp.

Example:
Set the cool down:
pawn Код:
PlayerLastUpgrade[playerid] = gettime();
Check if it's been less than a minute before the command was used:
pawn Код:
if(gettime() < (PlayerLastUpgrade[playerid] + 60))
Likewise you can do this to check if it's been a minute or longer:
pawn Код:
if(gettime() >= (PlayerLastUpgrade[playerid] + 60))
Instead of setting a variable when they can use the command, you set a variable of when they used the command. This allows you to check if the current time is far enough away from the time they used the command at. This also provides the advantage of being able to tell the player how much longer until they can use the command.

Detecting how many seconds before the command can be used again is as simple as:
pawn Код:
PlayerLastUpgrade[playerid] + 60 - gettime()
Reply
#3

How would I place this in then?
Reply
#4

How would I place this in?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)