Coding with two different players in one loop? (how to assign variables for checking?)
#1

Hello!

I made myself a new timer that loops through all players, something similiar as the OnPlayerUpdate; but this one is called every 1 second in my script.

But, my question is, i need to check if someone is wanted, and afterwards assign a variable giving me their playerid in the code further down.
i did it with the variable wantedid and ticketid to save their id's for later use.

But; this fails, and it seems like it's only the ID=0 who gets affected.

Can someone help me to recognize a wanted player in my code, when a cop is getting close to the wanted player? I am a bit stuck here :/

pawn Код:
public OnPlayerUpdate2()
{
    new ticketid = -1, wantedid = -1;

    foreach(new i : Player)
    {
       
        if(GetPlayerWantedLevel(i) == 1) //how to save the players into variables correctly, for use further down?
            ticketid = i;
        if(GetPlayerWantedLevel(i) > 1) //same here; i'm doing it the wrong way i think :S
            wantedid = i;
           
        if(ticketid != -1)
        {
           
            if(GetPVarInt(i, "copduty") == 1)
            {
               
                if(IsPlayerInAnyVehicle(i) || IsPlayerInAnyVehicle(ticketid))
                {
                   
                    if (GetPlayerVehicleID(i) != GetPlayerVehicleID(ticketid) || !IsPlayerInAnyVehicle(i) || !IsPlayerInAnyVehicle(ticketid))
                    {
                       
                        continue;
                    }
                   
                }
                else if(PlayerToPlayer(i, ticketid, 4) && GetPVarInt(ticketid, "afk") == 0 && tDialogOpen[ticketid] == 0 )
                {
                    if(checktime == 0)
                        checktime = gettime();
                    GameTextForPlayer(i, "~r~Giving ticket...", 1000, 1);
                    if(gettime() - checktime >= 3)
                    {
                        GameTextForPlayer(i, "~g~Ticket given, please wait...", 3000, 1);
                        TogglePlayerControllable(ticketid, 0);
                        ShowPlayerDialog(ticketid,ticketdialog,DIALOG_STYLE_MSGBOX,""cwhite"Pay Ticket: "cgreen"Yes "cwhite"or"cred" No?", ""cwhite"You have wanted level 1, and a cop just busted you.\nAre you willing to pay the ticket?\n"cgreen"PRICE:"cwhite" 10 000$ / 5 cookies / 5 score", "Yes", "No");
                        tDialogOpen[ticketid] = 1;
                        cop[i][tickets] ++;
                        checktime = 0;
                    }
                }
            }
        }
       
        if(wantedid != -1)
        {
            if(GetPVarInt(i, "copduty") == 1)
            {
                if(IsPlayerInAnyVehicle(i) && IsPlayerInAnyVehicle(wantedid))
                {
                   
                    if (GetPlayerVehicleID(i) != GetPlayerVehicleID(wantedid) || !IsPlayerInAnyVehicle(i) || !IsPlayerInAnyVehicle(wantedid))
                    {
                       
                        continue;
                    }
                }
                else if(PlayerToPlayer(i, wantedid, 2) && GetPVarInt(wantedid, "afk") != 1&& AlreadyWanted[wantedid] == 0)
                {
                    if(checktime == 0)
                        checktime = gettime() &&
                        GameTextForPlayer(i, "~r~Arresting...", 1000, 1);
                    if(gettime() - checktime >= 4)
                    {
                        AlreadyWanted[wantedid] = 1; //to prevent it to be called many times
                        GameTextForPlayer(i, "~g~Arrested player, jailing...", 4000, 1);
                        TogglePlayerControllable(wantedid, 0);
                         new Float:x, Float:y, Float:z, Float:a;
                         
                        GetPlayerPos(wantedid,x,y,z);
                        GetPlayerFacingAngle(wantedid, a);
                        SetPVarFloat(wantedid, "Xpos", x);
                        SetPVarFloat(wantedid, "Ypos", y);
                        SetPVarFloat(wantedid, "Zpos", z);
                        SetPVarFloat(wantedid, "Apos", a);
                        SetPVarInt(wantedid, "int", GetPlayerInterior(wantedid));
                        SetPVarInt(wantedid, "world", GetPlayerVirtualWorld(wantedid));
                       
                        ApplyAnimation(wantedid, "PED", "handsup", 4, 0 , 0, 0, 1, 5000);
                        SetPVarInt(wantedid, "Jailed", 1);
                        ResetPlayerWeapons(wantedid);
                        SetPVarInt(wantedid, "arrested", 1); //for the function call, to define if its /ar or /jail which is used.
                        new cash;
                            cash = random (10000);
                        new msg[180];
                        format(msg, sizeof(msg),"You jailed "cpurple"%s"cwhite" for "cpurple"%d"cwhite" minutes. You earned "cpurple"$%d"cwhite", good job officer!", PlayerName(wantedid), GetPlayerWantedLevel(wantedid), cash);
                        GivePlayerMoney(i, cash);
                        SendClientMessage(i, -1, msg);
                        format(msg, sizeof(msg),"Info: "cwhite"You got arrested by "cpurple"%s %s"cwhite" for "cpurple"%d"cwhite" minutes", CopRank(i), PlayerName(i), GetPlayerWantedLevel(wantedid));
                        SendClientMessage(wantedid, purple, msg);
                        SetTimerEx("putinjail",4000,false,"i",wantedid);
                        cop[i][arrests] ++ ;
                        checktime = 0;
                    }
                }
            }
        }
       
       
    }
    return 1;
}
Any explanation or further ideas will be very welcomed!

Thanks alot
Reply
#2

change it to :
pawn Код:
new wantedid[MAX_PLAYERS],ticketid[MAX_PLAYERS] // declare this on top of your script not inside your public OnPlayerUpdate2() (remove the variable wantedid and the ticketid cuz u already have a gloval var wantedid[MAX_PLAYERS] and ticketid[MAX_PLAYERS]

// and reset it everytime player disconnects
public OnPlayerDisconnect(playerid, reason)
{
      wantedid[playerid]=0;
      ticketid[playerid]=0;
}
Reply
#3

Quote:
Originally Posted by Quickie
Посмотреть сообщение
change it to :
pawn Код:
new wantedid[MAX_PLAYERS],ticketid[MAX_PLAYERS] // declare this on top of your script not inside your public OnPlayerUpdate2() (remove the variable wantedid and the ticketid cuz u already have a gloval var wantedid[MAX_PLAYERS] and ticketid[MAX_PLAYERS]

// and reset it everytime player disconnects
public OnPlayerDisconnect(playerid, reason)
{
      wantedid[playerid]=0;
      ticketid[playerid]=0;
}
I thought about this too; but;

wouldn't i have to declare their id's anyway? like this:

pawn Код:
if(GetPlayerWantedLevel(i) > 1)
   wantedid[i]= i;
or could i simply just say that "this player is wanted" because the value is 1?
pawn Код:
if(GetPlayerWantedLevel(i) > 1)
   wantedid[i]= 1;
This bit confuses me a bit, because i can't imagine it in my head as i do with many other things


EDIT: Because i can't do this:
pawn Код:
if(IsPlayerInAnyVehicle(ticketid[i]))
Or at least, i don't think you can. "i" is already looped through to check the cop?
Reply
#4

i agree with this
pawn Код:
if(GetPlayerWantedLevel(i) > 1)
   wantedid[i]= 1;// change it to wantedid[i]=GetPlayerWantedLevel(i); so you will configure his actual wanted level and store to his player variable wantedid[playerid]
// ur looping to connected players foreach(new i:Player) and does the "i" signifies the id of the
// current looped player so wantedid[i] is a clever move
//
//the isplayerinvehicle thing
// change it to
if(IsPlayerInAnyVehicle(i)) // as i just have said,,.. the "i" signifies the id of the current plyer looped

// im sorry cuz i just barely read your code and cant understand the rest 3/4 of it
// what does the ticketid and wantedid?
//it should be
if(ticketid == -1) // this checks if the player is a cop right ??? ive notice u write if(ticketid != -1)
if(wantedid==-1)// this one too???

//BTW i can provide you a better code instead of your onplayerupdate like timer
// make a command for cop player so he can ticket or arrest the wanted player
// like /ticket and /arrest
CMD:arrest(playerid,params[])
{
    new targetid,msg1[128],msg2[128];
    new Float:X,Float:Y,Float:Z;
    GetPlayerPos(playerid,X,Y,Z);
    if(iscop[playerid]!=1) // checks if the player enter this command is a cop then if its not he cant use this command
    {
        return SendClientMessage(playerid,-1,"only cops can use this command");
    }
    if(isnull(params) // if the cop just enter "/arrest"
    {
       
        foreach(new i:Player) // looping throuh all players
        {
            if(i==playerid)continue; // if loops reached the cops id then we'll skip
            if(IsPlayerInRangeOfPoint(i, 7.0, X, Y, Z)&&GetPlayerWantedLevel(i)) // checking if there is wanted player nearby
            {
               
                // ur command to arrest the player
                SendClientMessage(i,-1,"arrested for being wanted");
                return SendClientMessage(playerid,-1,"you have arrested a wanted player); // stopping the loop if we found the wantedplayer
            }
               
        }
        return SendClientMessage(playerid,-1,"
no wanted player nearby");// prints to the client if we cant find a wanted player nearby
    }
    // gets though here if the player type /arrest <id/name>
    if(sscanf(params,"
u",targetid)) // checks if the player enter a valid syntax
    {
        return SendClientMessage(playerid,-1,"
Syntax: /arrest <id/name>");
    }
    if(targetid==INVALID_PLAYER_ID) // checks if the player enters the invalid targetid eg.yourself and offline player
    {
        return SendClientMessage(playerid,-1,"
player not found");
    }
    if(IsPlayerInRangeOfPoint(targetid, 7.0, X, Y, Z)&&GetPlayerWantedLevel(playerid)) // checks if the player is wanted and is if the player nearby
    {
        // ur command to arrest the player
        SendClientMessage(i,-1,"
arrested for being wanted");
        return SendClientMessage(playerid,-1,"
you have arrested a wanted player);
    }
    else
    {
        return SendClientMessage(playerid,-1,"player must be near");
    }
    return 1;
}
// i hope this will help you
// goodluck
Reply
#5

Quote:
Originally Posted by Quickie
Посмотреть сообщение
i agree with this
pawn Код:
if(GetPlayerWantedLevel(i) > 1)
   wantedid[i]= 1;// change it to wantedid[i]=GetPlayerWantedLevel(i); so you will configure his actual wanted level and store to his player variable wantedid[playerid]
// ur looping to connected players foreach(new i:Player) and does the "i" signifies the id of the
// current looped player so wantedid[i] is a clever move
//
//the isplayerinvehicle thing
// change it to
if(IsPlayerInAnyVehicle(i)) // as i just have said,,.. the "i" signifies the id of the current plyer looped

// im sorry cuz i just barely read your code and cant understand the rest 3/4 of it
// what does the ticketid and wantedid?
//it should be
if(ticketid == -1) // this checks if the player is a cop right ??? ive notice u write if(ticketid != -1)
if(wantedid==-1)// this one too???

//BTW i can provide you a better code instead of your onplayerupdate like timer
// make a command for cop player so he can ticket or arrest the wanted player
// like /ticket and /arrest
CMD:arrest(playerid,params[])
{
    new targetid,msg1[128],msg2[128];
    new Float:X,Float:Y,Float:Z;
    GetPlayerPos(playerid,X,Y,Z);
    if(iscop[playerid]!=1) // checks if the player enter this command is a cop then if its not he cant use this command
    {
        return SendClientMessage(playerid,-1,"only cops can use this command");
    }
    if(isnull(params) // if the cop just enter "/arrest"
    {
       
        foreach(new i:Player) // looping throuh all players
        {
            if(i==playerid)continue; // if loops reached the cops id then we'll skip
            if(IsPlayerInRangeOfPoint(i, 7.0, X, Y, Z)&&GetPlayerWantedLevel(i)) // checking if there is wanted player nearby
            {
               
                // ur command to arrest the player
                SendClientMessage(i,-1,"arrested for being wanted");
                return SendClientMessage(playerid,-1,"you have arrested a wanted player); // stopping the loop if we found the wantedplayer
            }
               
        }
        return SendClientMessage(playerid,-1,"
no wanted player nearby");// prints to the client if we cant find a wanted player nearby
    }
    // gets though here if the player type /arrest <id/name>
    if(sscanf(params,"
u",targetid)) // checks if the player enter a valid syntax
    {
        return SendClientMessage(playerid,-1,"
Syntax: /arrest <id/name>");
    }
    if(targetid==INVALID_PLAYER_ID) // checks if the player enters the invalid targetid eg.yourself and offline player
    {
        return SendClientMessage(playerid,-1,"
player not found");
    }
    if(IsPlayerInRangeOfPoint(targetid, 7.0, X, Y, Z)&&GetPlayerWantedLevel(playerid)) // checks if the player is wanted and is if the player nearby
    {
        // ur command to arrest the player
        SendClientMessage(i,-1,"
arrested for being wanted");
        return SendClientMessage(playerid,-1,"
you have arrested a wanted player);
    }
    else
    {
        return SendClientMessage(playerid,-1,"player must be near");
    }
    return 1;
}
// i hope this will help you
// goodluck
Thanks for the code Quickie but based on my ideas and my script, i was actually going to avoid having commands to ticket and arrest players.

And a few corrections to the code you provided:
pawn Код:
// what does the ticketid and wantedid?
//it should be
if(ticketid == -1) // this checks if the player is a cop right ??? ive notice u write if(ticketid != -1)
if(wantedid==-1)// this one too???
if you have global variables, made as "per-player" variables, those should be:
pawn Код:
if(ticketid[i]) == 1 //did get the variable assigned as he was wanted with 1 wanted level.
Btw, you misunderstood my code, i don't think you can help so much then. But thanks for trying. (Based on the variables you said i had to create...). And if you don't know how to help, you shouldn't try, it only confuses people.

let me repeat my question for the forum:

I have a loop, and i want to do actions comparing "two" "i's" in the script, like two different players. How can i do this; is there any other way than in the code i provided firstly?
I want to check if the saved variable (the wanted playerid's ID) later in my code, so i can perform an arrest or ticket on the wanted player. It's hard to explain, so let me show by some code:

pawn Код:
else if(PlayerToPlayer(i, ticketid, 4)
if you see the "ticketid", you'll understand. I can't compare two "i" in the code, that would mean the same player. How can i set the variable / an variable to hold the playerid of the wanted player?
Any tips and tricks ?
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)