[Assistance needed] - Failed to loop through players.
#1

Hi. For some reason, i have failed with a loop through players.
This following code stops at ID:0 and won't continue to search for other players.

The idea is, that it should search for any players, who are in the same virtual world and in range of house location X,Y,Z.

Код:
	for(new i = 0; i<APPROX_PLAYERS; i++)
		{
		if( (GetPlayerVirtualWorld(i) == houselocW) && (IsPlayerInRangeOfPoint(i, 20, houselocX, houselocY, houselocZ)) )
		    {
			SendClientMessage(i, COLOR_YELLOW, "( I ) Riiing.. riing.");
			SendClientMessage(playerid, COLOR_YELLOW, "( I ) Biip.. biip.");
			return 1;
			}else{
			SendClientMessage(playerid, COLOR_YELLOW, "( I ) No answer");
			return 1;
			}
	    }
I will be very grateful if anyone can find the mistake i've made.
Reply
#2

Try removing return 1;
Reply
#3

I've tried changing it to the below, but it didn't help either. Should i completely remove return 1; ?

Код:
	for(new i = 0; i<APPROX_PLAYERS; i++)
		{
		if( (GetPlayerVirtualWorld(i) == houselocW) && (IsPlayerInRangeOfPoint(i, 20, houselocX, houselocY, houselocZ)) )
		    {
			SendClientMessage(i, COLOR_YELLOW, "( I ) Riiing.. riing.");
			SendClientMessage(playerid, COLOR_YELLOW, "( I ) Biip.. biip.");
			}else{
			SendClientMessage(playerid, COLOR_YELLOW, "( I ) No answer");
			}
            return 1;
	    }
Reply
#4

I see you are using a define called "APPROX_PLAYERS." Why do this, when you could easily use foreach? It (probably) does the same thing, but its (probably) much more efficient then how you are doing it with the define. You can do the same thing by doing this:

pawn Код:
foreach(Player, i)
Reply
#5

RealCop - I'm oldstyle!
Actually, i havn't seen foreach before now. Thanks. I'll look into it.

For the code, i removed all of the return 1;, and it works more or less.
It spams the no answer, and whenever a person is near, it says biip biip.


Now i just need to figure out, how exactly i'm gonna stop the spam.
Reply
#6

Quote:
Originally Posted by Faith
Посмотреть сообщение
RealCop - I'm oldstyle!
Actually, i havn't seen foreach before now. Thanks. I'll look into it.

For the code, i removed all of the return 1;, and it works more or less.
It spams the no answer, and whenever a person is near, it says biip biip.


Now i just need to figure out, how exactly i'm gonna stop the spam.
You need to break the loop I believe, don't quote me on that though!

pawn Код:
break;
Reply
#7

If i put break; right below biip..biip, then it will first break once there's a person in the house.
So it won't work, unfortunately.

The only solution i see, is that i remove the No Answer, message - Or i could check the room and if it's empty, it will give a no answer message.

Thinking while i'm writing.

Thanks for your help guys. I hope i can handle it from here.
Reply
#8

Faith, the no answer spam is caused by whenever your loop goes through an id that isn't in the virtual world it defaults to your else statement.. every time... here's my style of fixing this, put the no answer outside of the for loop, and before the loop make a variable, we can call it check. since when a variable is made it defaults to zero, so where you have the
Код:
SendClientMessage(i, COLOR_YELLOW, "( I ) Riiing.. riing.");
one line below that put check = 1; and after the loop have an if statement saying
Код:
 if (check != 1) SendClientMessage(playerid, COLOR_YELLOW, "( I ) No answer");
Reply
#9

EXCELLENT idea! Thanks Dowster. I'll do that right away!
Reply
#10

sure man, its what i've been doing in my scripts :P
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)