SA-MP Forums Archive
[Assistance needed] - Failed to loop through players. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: [Assistance needed] - Failed to loop through players. (/showthread.php?tid=267583)



[Assistance needed] - Failed to loop through players. - Faith - 09.07.2011

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.


Re: [Assistance needed] - Failed to loop through players. - Retardedwolf - 09.07.2011

Try removing return 1;


Re: [Assistance needed] - Failed to loop through players. - Faith - 09.07.2011

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;
	    }



Re: [Assistance needed] - Failed to loop through players. - Scenario - 09.07.2011

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)



Re: [Assistance needed] - Failed to loop through players. - Faith - 09.07.2011

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.


Re: [Assistance needed] - Failed to loop through players. - Scenario - 09.07.2011

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;



Re: [Assistance needed] - Failed to loop through players. - Faith - 09.07.2011

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.


Re: [Assistance needed] - Failed to loop through players. - dowster - 09.07.2011

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");



Re: [Assistance needed] - Failed to loop through players. - Faith - 09.07.2011

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


Re: [Assistance needed] - Failed to loop through players. - dowster - 09.07.2011

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