SA-MP Forums Archive
pick a random guy? - 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: pick a random guy? (/showthread.php?tid=209969)



pick a random guy? - MrIncredible - 11.01.2011

ok, there are 50 ppl in the server, and i want to /kick a random guy.. now is this how i get a random player?

Код:
for(new i; i<500; i++)if(IsPlayerConnected(i))
	{
		new r = random(i);
		Kick®;
	}
is this how it works?


Re: pick a random guy? - Kitten - 11.01.2011

Yes , why would u want to kick a random guy just asking sorryz.


Re: pick a random guy? - MrIncredible - 11.01.2011

That's not the point, the point is that I may not have the right code...


AW: pick a random guy? - Extremo - 11.01.2011

Why would you loop 500 times to kick 1 random guy?

pawn Код:
new r = random(CURRENT_PLAYERS);
Kick(r);
Suddenly there will be an error because it tries to kick id 2 but ID 2 doesn't exist... now we have a problem don't we?

So now, you look up ****** his foreach function, figure your way out on that one and you have a kick command that kicks a random dude =)

https://sampforum.blast.hk/showthread.php?tid=92679


Re: pick a random guy? - Kyosaur - 11.01.2011

This GetRandomPlayerID function is from ******, so i assume it works.

Код:
stock GetPlayerRandomID()
{
    // Check if there are actually any players
    if (!gPlayersOnline)
    {
        // No, return appropriately
        return INVALID_PLAYER_ID;
    }
    // Select which of the connected players to return
    new
        id = random(gPlayersOnline);
    // Find the player
    for (new i = 0; i < MAX_PLAYERS; i++)
    {
        // Loop through all players and only update if they're connected
        if (IsPlayerConnected(i))
        {
            if (id == 0)
            {
                // No players left to loop through
                // Return this ID
                return i;
            }
            // Not found the player we want yet, move on to the next
            id = id - 1;
            // While id is not 0 then we have id number of connected players left to skip
        }
    }
    // Shouldn't get here but needed by the compiler
    return INVALID_PLAYER_ID;
}
You just have to add the global variable "gPlayersOnline" and increment/decrement it when players connect and disconnect.


Re: pick a random guy? - Joe Staff - 11.01.2011

without using foreach you could do this

pawn Код:
stock GetRandomPlayerID()
{
    new op;
    new po[MAX_PLAYERS];
    for(new playerid;playerid<MAX_PLAYERS;playerid++)
    {
        if(!IsPlayerConnected(playerid))continue;
        po[op]=playerid; // HAHAHHA POOP!
        op++;
    }
    return po[random(op)];
}