Adding a timer between robberies .
#1

Below you can find one of the rob commands , and i would like to know how can i make a rob timer so a player won't type the same command 30000 times and ruin the server economy .
pawn Код:
CMD:robstore(playerid,params[])
{
    if(gTeam[playerid] == TEAM_COP) return SendClientMessage(playerid,COLOR_RED,"{00FFFF}[SERVER]{EEEEEE}Law Enforcement Officers cannot commit a robbery crime");
    new pName[MAX_PLAYER_NAME];
    new string[128];
    GetPlayerName(playerid,pName,sizeof(pName));
    if(GetPlayerWantedLevel(playerid >=1)) return SendClientMessage(playerid,COLOR_RED,"{00FFFF}[SERVER]{EEEEEE}You cant rob a store with a wanted level.");
    if(IsInside247[playerid] == 1)
    {
    new StoreMoney = RandomEx( 10000,30000);
    format(string,sizeof(string),"{FF0000}[ROBBERY]{EEEEEE}%s has robbed the 24/7 store of Fort Carson and got %d$",pName,StoreMoney);
    SendClientMessageToAll(COLOR_LIGHTGREEN,string);
    GameTextForPlayer(playerid,"~y~24/7Store~n~~r~ROBBED",3000,5);
    format(string,sizeof(string),"{FF0000}[ROBBERY]{EEEEEE}You've successfully robbed the 24/7 store and got $%d",pName,StoreMoney);
    SendClientMessage(playerid,COLOR_LIGHTGREEN,string);
    SendClientMessage(playerid,COLOR_RED,"{00FFFF}[SERVER]{EEEEEE}You have gained 3 stars for robbing the 24/7 store");
    GivePlayerMoney(playerid,StoreMoney);
    SetPlayerWantedLevel(playerid,3);
    }
    else SendClientMessage(playerid,COLOR_RED,"{00FFFF}[SERVER]{EEEEEE}You need to be inside the 247/7 store");
    return 1;
}
Reply
#2

pawn Код:
#define PF:%0(%1)   forward %0(%1); \
                    public %0(%1)

CMD:robstore(playerid,params[])
{
    if(gTeam[playerid] == TEAM_COP) return SendClientMessage(playerid,COLOR_RED,"{00FFFF}[SERVER]{EEEEEE}Law Enforcement Officers cannot commit a robbery crime");
    new pName[MAX_PLAYER_NAME];
    new string[128];
    GetPlayerName(playerid,pName,sizeof(pName));
    if(GetPlayerWantedLevel(playerid >=1)) return SendClientMessage(playerid,COLOR_RED,"{00FFFF}[SERVER]{EEEEEE}You cant rob a store with a wanted level.");
    if(IsInside247[playerid] == 1 && GetPVarInt(playerid, "RobWait") != 1)
    {
        new StoreMoney = RandomEx( 10000,30000);
        format(string,sizeof(string),"{FF0000}[ROBBERY]{EEEEEE}%s has robbed the 24/7 store of Fort Carson and got %d$",pName,StoreMoney);
        SendClientMessageToAll(COLOR_LIGHTGREEN,string);
        GameTextForPlayer(playerid,"~y~24/7Store~n~~r~ROBBED",3000,5);
        format(string,sizeof(string),"{FF0000}[ROBBERY]{EEEEEE}You've successfully robbed the 24/7 store and got $%d",pName,StoreMoney);
        SendClientMessage(playerid,COLOR_LIGHTGREEN,string);
        SendClientMessage(playerid,COLOR_RED,"{00FFFF}[SERVER]{EEEEEE}You have gained 3 stars for robbing the 24/7 store");
        GivePlayerMoney(playerid,StoreMoney);
        SetPlayerWantedLevel(playerid,3);
        SetPVarInt(playerid, "RobWait", 1);
        SetTimerEx("RobTime", TIME, false, "i", playerid); // Change Time to how ever long you want.
    }
    else SendClientMessage(playerid,COLOR_RED,"{00FFFF}[SERVER]{EEEEEE}You are either not in a 24-7 or have already robbed this store recently.");
    return 1;
}

PF:RobTime(playerid)
{
    SendClientMessage(playerid, -1, #You may now Rob the store again);
    DeletePVar(playerid, "RobWait");
    return 1;
}
Reply
#3

Quote:
Originally Posted by Lynn
Посмотреть сообщение
pawn Код:
#define PF:%0(%1)   forward %0(%1); \
                    public %0(%1)

CMD:robstore(playerid,params[])
{
    if(gTeam[playerid] == TEAM_COP) return SendClientMessage(playerid,COLOR_RED,"{00FFFF}[SERVER]{EEEEEE}Law Enforcement Officers cannot commit a robbery crime");
    new pName[MAX_PLAYER_NAME];
    new string[128];
    GetPlayerName(playerid,pName,sizeof(pName));
    if(GetPlayerWantedLevel(playerid >=1)) return SendClientMessage(playerid,COLOR_RED,"{00FFFF}[SERVER]{EEEEEE}You cant rob a store with a wanted level.");
    if(IsInside247[playerid] == 1 && GetPVarInt(playerid, "RobWait") != 1)
    {
        new StoreMoney = RandomEx( 10000,30000);
        format(string,sizeof(string),"{FF0000}[ROBBERY]{EEEEEE}%s has robbed the 24/7 store of Fort Carson and got %d$",pName,StoreMoney);
        SendClientMessageToAll(COLOR_LIGHTGREEN,string);
        GameTextForPlayer(playerid,"~y~24/7Store~n~~r~ROBBED",3000,5);
        format(string,sizeof(string),"{FF0000}[ROBBERY]{EEEEEE}You've successfully robbed the 24/7 store and got $%d",pName,StoreMoney);
        SendClientMessage(playerid,COLOR_LIGHTGREEN,string);
        SendClientMessage(playerid,COLOR_RED,"{00FFFF}[SERVER]{EEEEEE}You have gained 3 stars for robbing the 24/7 store");
        GivePlayerMoney(playerid,StoreMoney);
        SetPlayerWantedLevel(playerid,3);
        SetPVarInt(playerid, "RobWait", 1);
        SetTimerEx("RobTime", TIME, false, "i", playerid); // Change Time to how ever long you want.
    }
    else SendClientMessage(playerid,COLOR_RED,"{00FFFF}[SERVER]{EEEEEE}You are either not in a 24-7 or have already robbed this store recently.");
    return 1;
}

PF:RobTime(playerid)
{
    SendClientMessage(playerid, -1, #You may now Rob the store again);
    DeletePVar(playerid, "RobWait");
    return 1;
}
Why use pvars in that ? a new and a timer would be enough .
Anybody else ?
Reply
#4

PVars automatically destroy on Disconnect, and there is literally no reason to create a global variable, for something like that. The code Posted will work fine, why would you want something else.
But because you're so unhappy with PVars....
pawn Код:
forward RobTime(playerid);

static bool: RobbingStore[MAX_PLAYERS];

public OnPlayerConnect(playerid)
{
    RobbingStore[playerid] = false;
    return 1;
}

CMD:robstore(playerid,params[])
{
    if(gTeam[playerid] == TEAM_COP) return SendClientMessage(playerid,COLOR_RED,"{00FFFF}[SERVER]{EEEEEE}Law Enforcement Officers cannot commit a robbery crime");
    new pName[MAX_PLAYER_NAME];
    new string[128];
    GetPlayerName(playerid,pName,sizeof(pName));
    if(GetPlayerWantedLevel(playerid >=1)) return SendClientMessage(playerid,COLOR_RED,"{00FFFF}[SERVER]{EEEEEE}You cant rob a store with a wanted level.");
    if(IsInside247[playerid] == 1 && RobbingStore[playerid] == false)
    {
        new StoreMoney = RandomEx( 10000,30000);
        format(string,sizeof(string),"{FF0000}[ROBBERY]{EEEEEE}%s has robbed the 24/7 store of Fort Carson and got %d$",pName,StoreMoney);
        SendClientMessageToAll(COLOR_LIGHTGREEN,string);
        GameTextForPlayer(playerid,"~y~24/7Store~n~~r~ROBBED",3000,5);
        format(string,sizeof(string),"{FF0000}[ROBBERY]{EEEEEE}You've successfully robbed the 24/7 store and got $%d",pName,StoreMoney);
        SendClientMessage(playerid,COLOR_LIGHTGREEN,string);
        SendClientMessage(playerid,COLOR_RED,"{00FFFF}[SERVER]{EEEEEE}You have gained 3 stars for robbing the 24/7 store");
        GivePlayerMoney(playerid,StoreMoney);
        SetPlayerWantedLevel(playerid,3);
         RobbingStore[playerid] = true;
        SetTimerEx("RobTime", TIME, false, "i", playerid); // Change Time to how ever long you want.
    }
    else SendClientMessage(playerid,COLOR_RED,"{00FFFF}[SERVER]{EEEEEE}You are either not in a 24-7 or have already robbed this store recently.");
    return 1;
}

public RobTime(playerid)
{
    SendClientMessage(playerid, -1, #You may now Rob the store again);
    RobbingStore[playerid] = false;
    return 1;
}
public OnPlayerDisconnect(playerid)
{
    RobbingStore[playerid] = false;
    return 1;
}
You now have more lines, that are not needed and could be avoided by a simple Pvar

Quote:
Originally Posted by Johnson_Brooks
Посмотреть сообщение
Why use pvars in that ? a new and a timer would be enough .
Anybody else ?
Then why not do it yourself....
Reply
#5

Quote:
Originally Posted by Lynn
Посмотреть сообщение
PVars automatically destroy on Disconnect, and there is literally no reason to create a global variable, for something like that. The code Posted will work fine, why would you want something else.
But because you're so unhappy with PVars....
pawn Код:
forward RobTime(playerid);

static bool: RobbingStore[MAX_PLAYERS];

public OnPlayerConnect(playerid)
{
    RobbingStore[playerid] = false;
    return 1;
}

CMD:robstore(playerid,params[])
{
    if(gTeam[playerid] == TEAM_COP) return SendClientMessage(playerid,COLOR_RED,"{00FFFF}[SERVER]{EEEEEE}Law Enforcement Officers cannot commit a robbery crime");
    new pName[MAX_PLAYER_NAME];
    new string[128];
    GetPlayerName(playerid,pName,sizeof(pName));
    if(GetPlayerWantedLevel(playerid >=1)) return SendClientMessage(playerid,COLOR_RED,"{00FFFF}[SERVER]{EEEEEE}You cant rob a store with a wanted level.");
    if(IsInside247[playerid] == 1 && RobbingStore[playerid] == false)
    {
        new StoreMoney = RandomEx( 10000,30000);
        format(string,sizeof(string),"{FF0000}[ROBBERY]{EEEEEE}%s has robbed the 24/7 store of Fort Carson and got %d$",pName,StoreMoney);
        SendClientMessageToAll(COLOR_LIGHTGREEN,string);
        GameTextForPlayer(playerid,"~y~24/7Store~n~~r~ROBBED",3000,5);
        format(string,sizeof(string),"{FF0000}[ROBBERY]{EEEEEE}You've successfully robbed the 24/7 store and got $%d",pName,StoreMoney);
        SendClientMessage(playerid,COLOR_LIGHTGREEN,string);
        SendClientMessage(playerid,COLOR_RED,"{00FFFF}[SERVER]{EEEEEE}You have gained 3 stars for robbing the 24/7 store");
        GivePlayerMoney(playerid,StoreMoney);
        SetPlayerWantedLevel(playerid,3);
         RobbingStore[playerid] = true;
        SetTimerEx("RobTime", TIME, false, "i", playerid); // Change Time to how ever long you want.
    }
    else SendClientMessage(playerid,COLOR_RED,"{00FFFF}[SERVER]{EEEEEE}You are either not in a 24-7 or have already robbed this store recently.");
    return 1;
}

public RobTime(playerid)
{
    SendClientMessage(playerid, -1, #You may now Rob the store again);
    RobbingStore[playerid] = false;
    return 1;
}
public OnPlayerDisconnect(playerid)
{
    RobbingStore[playerid] = false;
    return 1;
}
You now have more lines, that are not needed and could be avoided by a simple Pvar



Then why not do it yourself....
I asked a question , and i did not force you to make me this code . If i could make it my self i would .
Thanks for your help , i didn't want to make you feel bad .
Reply
#6

pawn Код:
CMD:robstore(playerid,params[])
{
    if(gTeam[playerid] == TEAM_COP) return SendClientMessage(playerid, COLOR_RED, "{00FFFF}[SERVER]{EEEEEE}Law Enforcement Officers cannot commit a robbery crime");
    if(GetPlayerWantedLevel(playerid)) return SendClientMessage(playerid,COLOR_RED,"{00FFFF}[SERVER]{EEEEEE}You can't rob a store with a wanted level.");
    if(gettime() < (RobTime[playerid] + 60)) return SendClientMessage(playerid, COLOR_RED, "{00FFFF}[SERVER]{EEEEEE}You must wait 1 minute before robbing a store again.");
    if(!IsInside247[playerid]) return SendClientMessage(playerid,COLOR_RED,"{00FFFF}[SERVER]{EEEEEE}You need to be inside the 247/7 store");
    new string[110], pName[MAX_PLAYER_NAME], StoreMoney = RandomEx(10000, 30000);
    GetPlayerName(playerid, pName, sizeof(pName));
    format(string, sizeof(string), "{FF0000}[ROBBERY]{EEEEEE}%s has robbed the 24/7 store of Fort Carson and got %d$", pName, StoreMoney);
    SendClientMessageToAll(COLOR_LIGHTGREEN,string);
    GameTextForPlayer(playerid,"~y~24/7Store~n~~r~ROBBED",3000,5);
    format(string, sizeof(string), "{FF0000}[ROBBERY]{EEEEEE}You've successfully robbed the 24/7 store and got $%d", pName,StoreMoney);
    SendClientMessage(playerid, COLOR_LIGHTGREEN, string);
    SendClientMessage(playerid, COLOR_RED, "{00FFFF}[SERVER]{EEEEEE}You have gained 3 stars for robbing the 24/7 store");
    GivePlayerMoney(playerid, StoreMoney);
    SetPlayerWantedLevel(playerid, 3);
    RobTime[playerid] = gettime();
    return 1;
}
At the top of your script:
pawn Код:
RobTime[MAX_PLAYERS];
pawn Код:
public OnPlayerConnect(playerid)
{
    RobTime[playerid] = 0;
    //Rest of code..
    return 1;
}
--

Gettime() returns a timestamp which is used to count the second from some midnight in 1970 or something. Read more here: https://sampwiki.blast.hk/wiki/Gettime

Basically, what this code does; Notice this check:
pawn Код:
if(gettime() < (RobTime[playerid] + 60))
This is saying: If the current number of seconds is less than (The last time the player robbed + 60 seconds [1 minute]), aka the player has robbed a store less than 60 seconds ago.

--

PVars are only recommended for when using variables between filterscripts and gamemodes, not necessarily for replacing global or local variables.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)