pick a random guy?
#1

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

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

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

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

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

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)];
}
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)