Adding countdown to this cmd
#1

Hello,i maked this rob command:

pawn Код:
CMD:rob(playerid, params[])
{
    if(GetPVarInt(playerid,"RobTime")>GetTickCount())return SendClientMessage(playerid,0xFF0000FF,"Please wait 60 seconds before rob again.");
    SetPVarInt(playerid,"RobTime",GetTickCount()+60000);
    new plwl = GetPlayerWantedLevel(playerid);
    plwl = GetPlayerWantedLevel(playerid);
    SetPlayerWantedLevel(playerid,plwl +2);
    new string[135];
    format(string, sizeof(string), "- ROBBERY IN PROGRESS - Wanted Level %d - Police have been advised.",plwl);
    SendClientMessage(playerid,red,string);
    GameTextForPlayer(playerid, "Robbery ~r~in progress.Do ~r~NOT leave the icon.", 30000, 5);
    new robmoney = 10500 + random(50000);
    GivePlayerMoney(playerid,robmoney);
    new string2[135];
    format(string2, sizeof(string2), "- SUCCESS ROBBERY - Wanted Level %d - Robbed %i$",plwl,robmoney);
    SendClientMessage(playerid,red,string2);
    GameTextForPlayer(playerid, "Robbery ~r~COMPLETE.", 3000, 5);
    SetPlayerScore(playerid, GetPlayerScore(playerid) + 1);
    new string3[135];
    new name[MAX_PLAYER_NAME];
    GetPlayerName(playerid, name, sizeof(name));
    format(string3, sizeof(string3), "- ROBBERY - %s (%d) has robbed %i$ from ",name,playerid,robmoney);
    SendClientMessageToAll(COLOR_WHITE,string3);
    return true;
}
And i want add the 30 seconds countdown to finish the rob,how to?
Reply
#2

Add a timer, which will be called after 30 seconds and will finish the robbery for player.
https://sampwiki.blast.hk/wiki/SetTimerEx
Reply
#3

Use a timer (SetTimerEx) and set the interval to 1000MS (one second). Create a global array, then add to that variable on the timer. Simple example:

pawn Код:
new pRobCount[MAX_PLAYERS];
new pRobTimerID[MAX_PLAYERS];

// In the command:
pRobTimerID[playerid] = SetTimerEx("pRobTimer", 1000, true, "i", playerid);
pRobCount[playerid] = 30; // How many seconds

forward pRobTimer(playerid);
public pRobTimer(playerid)
{
    pRobCount[playerid]--; // Take a second
    new string[128];
    format(string, sizeof(string), "%i seconds until robbery complete", RobCount[playerid]);
    SendClientMessage(playerid, -1, string);
    if(pRobCount[playerid] == 0)
    {
        KillTimer(pRobTimerID[playerid]);
        // Time has passed
    }
}
NOTE: Not compiled/tested.
Reply
#4

Thank you,it compiles with no errors.Here is my rob cmd now:

pawn Код:
CMD:rob(playerid, params[])
{
    if(GetPVarInt(playerid,"RobTime")>GetTickCount())return SendClientMessage(playerid,0xFF0000FF,"Please wait 60 seconds before rob again.");
    SetPVarInt(playerid,"RobTime",GetTickCount()+60000);
    new plwl = GetPlayerWantedLevel(playerid);
    plwl = GetPlayerWantedLevel(playerid);
    SetPlayerWantedLevel(playerid,plwl +2);
    pRobTimerID[playerid] = SetTimerEx("pRobTimer", 1000, true, "i", playerid);
    pRobCount[playerid] = 30;
    new string[135];
    format(string, sizeof(string), "- ROBBERY IN PROGRESS - Wanted Level %d - Police have been advised.",plwl);
    SendClientMessage(playerid,red,string);
    GameTextForPlayer(playerid, "Robbery ~r~in progress.Do ~r~NOT leave the icon.", 30000, 5);
    new robmoney = 10500 + random(50000);
    GivePlayerMoney(playerid,robmoney);
    new string2[135];
    format(string2, sizeof(string2), "- SUCCESS ROBBERY - Wanted Level %d - Robbed %i$",plwl,robmoney);
    SendClientMessage(playerid,red,string2);
    GameTextForPlayer(playerid, "Robbery ~r~COMPLETE.", 3000, 5);
    SetPlayerScore(playerid, GetPlayerScore(playerid) + 1);
    new string3[135];
    new name[MAX_PLAYER_NAME];
    GetPlayerName(playerid, name, sizeof(name));
    format(string3, sizeof(string3), "- ROBBERY - %s (%d) has robbed %i$ from ",name,playerid,robmoney);
    SendClientMessageToAll(COLOR_WHITE,string3);
    return true;
}
But the timer doesn't work,it shows the "Robbery complete" gametext,that need to show AFTER the timer finishes...
Reply
#5

The 'robbery complete' code should be where I put // Time has passed.

You should also use KillTimer(pRobTimerID[playerid]); if they leave the icon.
Reply
#6

Thank you.It's all working now (:

+rep.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)