Random player
#1

How do I make choose a random player and move it to another team ? or a team balance kinda thing ?
Reply
#2

anyone ?
Reply
#3

pawn Code:
new randomp;
randomp = random(MAX_PLAYERS);
gTeam[randomp] = TEAM; //Change this to your variables
SpawnPlayer(randomp);
Add this somewhere in your script. I don't know if this is working, I just made it :P
Try it ouy
Reply
#4

NO!

max players is 500.
i dont think you run a server with 500 players (do you?)
Code:
new randomp;
randomp = random(GetMaxPlayers());
gTeam[randomp] = TEAM; //Change this to your variables
SpawnPlayer(randomp);
GetMaxPlayers will read in config.cfg what you have set there.
Reply
#5

Yeah I didn't think: On this way you have fast an invalid player. Try this :P (3 time's is the charm)

pawn Code:
new RandomP;
RandomP = random(GetMaxPlayers());
if(!IsPlayerConnected(RandomP)) return false;
gTeam[RandomP] = team; //Change to your variables
SpawnPlayer(RandomP);
Reply
#6

Just in case you use foreach theres a nice random player function that comes with it.
pawn Code:
new randomplayer = Iter_Random(Player);
Reply
#7

untested but i would go like this
pawn Code:
public GetRandomPlayer(){          //returns random playerid or -1 if no players
    new test,test2,players[MAX_PLAYERS];
    for(new i=0;i<MAX_PLAYERS;i++){
        if(IsPlayerConnected(i)){
            players[test] = i;
            test++;
        }
    }
    if(test == 0)return -1;
    else test2 = random(test);
    return players[test2];
}
fine dont think it makes much difference however -1 it is
Reply
#8

But 0 is a playerid i think it would be safer returning -1 if no players are found. Na won't make much difference just thought i'd point it out. Most of the time there probably would have to be a player online to use the function (unless timers).
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)