20.07.2017, 07:49
How to make a timer when I use CMD the cmd wait few seconds and after that preform the action?
Like steal timer...
+Rep helper
Like steal timer...
+Rep helper
CMD:cmdname(playerid, params[])
{
SetTimerEx("CommandTimer", Time, 0, "i", playerid); //Run a timer. When the command executes
return 1;
}
forward CommandTimer(playerid);
public CommandTimer(playerid)
{
//The things you want to do.
}
//On top of your script
new StealTimer[MAX_PLAYERS]; //Declaring a variable to store the player timers in
new StealCount[MAX_PLAYERS]; //Declaring a variable to check where the timer has reached (counting)
CMD:steal(playerid) //A command using ZCMD include
{
StealTimer[playerid] = SetTimerEx("StealFromPlayer", 1000, true, "i", playerid); //Here we assigned the variable StealTimer to a player timer which repeats every one second
StealCount[playerid] = 5; //CHANGE THIS > Assign the time in seconds to do the action after (countdown variable)
new counttext[25]; //The following 3 lines just to print to the player a message on where the count has reached
format(counttext, sizeof(counttext), "~r~~h~%i Seconds Remaining", StealCount);
GameTextForPlayer(playerid, counttext, 1000, 4);
return 1;
}
forward StealFromPlayer(playerid); //Forwarded the function of the timer
public StealFromPlayer(playerid)
{
StealCount[playerid]--; //We decrese the counter every one second by one
if(StealCount[playerid] == 0) //This is if 5 seconds passed to the count (reached 0)
{
KillTimer(StealTimer[playerid]); //Don't forget to kill the timer so it doesn't stay on after you have done your things
GameTextForPlayer(playerid, "~g~~h~Done", 1000, 4);
//Robbed from player (YOUR CODES)
}
else //If the count didn't reach 0 yet. print him the remaining time
{
new counttext[25];
format(counttext, sizeof(counttext), "~r~~h~%i Seconds Remaining", StealCount);
GameTextForPlayer(playerid, counttext, 1000, 4);
}
return 1;
}
Here is a mini tutorial: (You can still copy paste but I suggest you to understand it)
PHP код:
|
CMD:steal(playerid)
{
new Float:x, Float:y, Float:z;
GetPlayerPos(playerid, x, y, z); //Get the player position when he starts to rob
StealTimer[playerid] = SetTimerEx("StealFromPlayer", 1000, true, "ifff", playerid, x, y, z); //Passed the float of the coordinations
return 1;
}
forward StealFromPlayer(playerid, Float:x, Float:y, Float:z); //Notice you have to forward them and add Float: tag before them
public StealFromPlayer(playerid, Float:x, Float:y, Float:z) //Here too
{
if(!IsPlayerInRangeOfPoint(playerid, 1.0, x, y, z)) //Here we check every after every second passed if the player in the range of his old position. decrese the range (1.0) to lesser number if you don't want him to move at all
{
KillTimer(StealTimer[playerid]); //As always kill the timer
GameTextForPlayer(playerid, "~r~~h~Failed to rob", 1000, 4);
return 1; //we return 1 so it doesn't continue the codes below
}
}
Add/Replace to the following
PHP код:
|