SA-MP Forums Archive
[HELP] How to choose a random player? - 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)
+--- Thread: [HELP] How to choose a random player? (/showthread.php?tid=281692)



[HELP] How to choose a random player? - TheBetaFox - 07.09.2011

Hey, guys.

Got any idea as to how would I choose a random player out of those currently online?
Thank you.


Re: [HELP] How to choose a random player? - Sasino97 - 07.09.2011

pawn Code:
new player = random(MAX_PLAYERS);
while(!IsPlayerConnected(player)) player = random(MAX_PLAYERS);
printf("Player ID %d", player);



Re: [HELP] How to choose a random player? - Shelby - 07.09.2011

I advice you to use foreach, it's much faster and more efficient.
Using:
pawn Code:
#include <foreach>
SendClientMessage(Iter_Random(Player), -1, "Hello, you have been selected by a random test!");
You can find foreach include here: https://sampforum.blast.hk/showthread.php?tid=92679


Re: [HELP] How to choose a random player? - RyDeR` - 07.09.2011

Quote:
Originally Posted by [GF]Sasino97
View Post
pawn Code:
new player = random(MAX_PLAYERS);
while(!IsPlayerConnected(player)) player = random(MAX_PLAYERS);
printf("Player ID %d", player);
And what if none is in the server? The loop will never end and thus everything will just freeze up.


Re: [HELP] How to choose a random player? - TheBetaFox - 07.09.2011

Thanks! @Larceny, with foreach, how can I do it so every player OTHER than the selected one (his ID is stored in a variable, btw) is affected by something?


Re: [HELP] How to choose a random player? - Sasino97 - 07.09.2011

Quote:
Originally Posted by RyDeR`
View Post
And what if none is in the server? The loop will never end and thus everything will just freeze up.
Oops, I didn't thought it :P