SA-MP Forums Archive
help/pick 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)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: help/pick a random player (/showthread.php?tid=267465)



help/pick a random player - RowdyrideR - 08.07.2011

hi guys..

i'm asking about how to pick a random player from the server!

like i have a president class and police and ......
and when the president dies,i want to set a random player as the president(president Class)!

anybody knows how?!

Thx alot guys...


Re: help/pick a random player - RowdyrideR - 08.07.2011

how?!?!?


Re: help/pick a random player - =WoR=Varth - 09.07.2011

With foreach:
pawn Код:
Iter_Random(Player);



Re: help/pick a random player - RowdyrideR - 09.07.2011

?! what you mean?!
i didnt get it :X
thx..


AW: help/pick a random player - Nero_3D - 09.07.2011

foreach is an include which provides faster loops, it also includes a function to get a random player

Just read the foreach topic and if you like it you could use the code varthshenon provided

If not use that

pawn Код:
stock GetRandomPlayer() {
    new
        i = 0,
        count = 0,
        player[MAX_PLAYERS];
    for( ; i != MAX_PLAYERS; ++i) {
        if(IsPlayerConnected(i)) {
            player[count++] = i;
        }
    }
    if(count == 0) {
        return INVALID_PLAYER_ID;
    }
    return player[random(count)];
}