[HELP] How to choose a random player?
#1

Hey, guys.

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

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

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
Reply
#4

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.
Reply
#5

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?
Reply
#6

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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)