What's wrong?
#1

So, I want to show how many players are being wanted in an textdraw. But, for some IDs it works, and for some not.

pawn Код:
new szInfo[74 + 1], iPlayersWanted = 0;
    foreach (new i: Player)
    {
        if (GetPlayerWantedLevel(i) > 0) ++ iPlayersWanted;
        if (iClass[i] == ClassPolice)
        {
            format(szInfo, 75, "~b~%d ~w~ieskomu zaideju. Norint suiimti/duoti bauda, spauskite ~r~MMB", iPlayersWanted);
            TextDrawSetString(TD_JobInfo[i], szInfo);
        }
    }
Reply
#2

Can you gimme the whole pwan code of TD_JobInfo?
Reply
#3

pawn Код:
TD_JobInfo[i] = TextDrawCreate(97.000000, 430.000000, "_");
        TextDrawBackgroundColor(TD_JobInfo[i], 255);
        TextDrawFont(TD_JobInfo[i], 2);
        TextDrawLetterSize(TD_JobInfo[i], 0.329999, 1.000000);
        TextDrawColor(TD_JobInfo[i], -1);
        TextDrawSetOutline(TD_JobInfo[i], 1);
        TextDrawSetProportional(TD_JobInfo[i], 1);
With TD showing everything is good, but for example. There's two players in the server (A (ID 0), B (ID 1)). Both are wanted, so for A its showing that there is 1 wanted player, and for B everything is good - its shows that 2 players are being wanted
Reply
#4

Okay, the problem here is with your logic. When you loop through the players, you check if the person is a cop and show all the wanted players inside the loop. Since looping starts from ID 0, this will only pick up the wanted players before the ID of a given police officer.

You can do this properly by first looping through the players and getting the wanted players, then loop through the players again to display the list of wanted players to the police officers.

pawn Код:
new szInfo[74 + 1], iPlayersWanted = 0;

foreach(new i: Player) {
    if(GetPlayerWantedLevel(i) > 0) {
        iPlayersWanted++;
    }
}

foreach(new i: Player {
    if (iClass[i] == ClassPolice)
    {
        format(szInfo, 75, "~b~%d ~w~ieskomu zaideju. Norint suiimti/duoti bauda, spauskite ~r~MMB", iPlayersWanted);
        TextDrawSetString(TD_JobInfo[i], szInfo);
    }
}
PS: There are probably more efficient ways of doing this, but this is the easiest way to explain what you are doing wrong.
Reply
#5

OK, thank you.. And what's wrong with this code?

pawn Код:
if (PRESSED(KEY_ACTION))
    {
        if (iClass[playerid] != ClassPolice || iCopStop[playerid] + 3000 > GetTickCount()) return true;
        new Float: fX, Float: fY, Float: fZ;
        GetPlayerPos(playerid, fX, fY, fZ);
        foreach (new i: Player)
        {
            if (IsPlayerInRangeOfPoint(i, 5, fX, fY, fZ))
            {
                if (GetPlayerWantedLevel(i) > 0)
                {
                    GameTextForPlayer(i, "~b~POLICIJA:~n~~b~PRASOME SUSTOTI, ARBA BUS IMTASI KITU VEIKSMU!", 3000, 4);
                    iCopStop[playerid] = GetTickCount();
                }
                else ShowInfoBox(playerid, "~w~Sis zaidлjas ~g~nera ~w~ieskomas!");
            }
            else ShowInfoBox(playerid, "~w~Salia jusu ~r~nera ~w~ieskomu zaideju!");
        }
    }
When I am near wanted player, it says that there isnt wanted players near me.
Reply
#6

^^, soz
Reply
#7

You really shouldn't be using else statements in player loops. For instance, if there are 100 people on your server and only one of them is within 5 units of you, it'll spam "~w~Salia jusu ~r~nera ~w~ieskomu zaideju!" 99 times. It only says there isn't a wanted player near you because at the time, the loop was going through your ID. You need to make sure you exclude the player pressing the key from the loop.

pawn Код:
if(PRESSED(KEY_ACTION)) {
        if(iClass[playerid] != ClassPolice || iCopStop[playerid] + 3000 > GetTickCount()) return 1;
       
        new Float: fX, Float: fY, Float: fZ, iWantedFound = 0;
        GetPlayerPos(playerid, fX, fY, fZ);
       
        foreach(new i: Player) {
            if(i != playerid && GetPlayerWantedLevel(i) > 0) { // Exclude the player himself + Run the wanted level code first, so it only checks against people who are actually wanted.
                if(IsPlayerInRangeOfPoint(i, 5, fX, fY, fZ)) {
               
                    GameTextForPlayer(i, "~b~POLICIJA:~n~~b~PRASOME SUSTOTI, ARBA BUS IMTASI KITU VEIKSMU!", 3000, 4);
                    iCopStop[playerid] = GetTickCount();
                    iWantedFound = 1; // Change WantedFound to 1, to indicate that we did find a wanted player.
                    break; // Exit the loop so it doesn't arrest more than one person.
                   
                }
            }
        }
       
        if(!iWantedFound) { // We did not find any wanted players near the player, send error message.
            ShowInfoBox(playerid, "~w~Sis zaidлjas ~g~nera ~w~ieskomas!");
        }
    }
I've commented the alterations I made so you can understand the logic behind the code properly. Hope it helps.
James Carson.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)