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: Random player! (
/showthread.php?tid=243371)
Random player! -
GNGification - 22.03.2011
Hello SA-MP Forums.
Today I have been making a new script and now I need to find out how can I choose someone randomly?
So like, there is 10 players online and function "FirstInfected" starts, it has to choose 1 random player and change he's team to SetPlayerTeam(playerid, 1);
Thank you allready!
Re: Random player! -
Mike Garber - 22.03.2011
pawn Код:
// Anywhere in your script (i just copied this from another topic right now)
GetPlayersOnServer() {
new count;
for(new x=0; x< MAX_PLAYERS; x++) { //x = MAX_PLAYERS
if(IsPlayerConnected(x))) {
count++;
}
}
return count;
}
// Then do;
new randomid = random(GetPlayersOnServer());
Re: Random player! -
GNGification - 22.03.2011
Okey another question:
forward FirstInfected();
public FirstInfected(playerid)
{
new randomid = random(GetPlayersOnServer());
GetPlayersOnServer()
{
new count;
for(new x=0; x< MAX_PLAYERS; x++)
{
if(IsPlayerConnected(x)))
{
count++;
}
}
return count;
}
Whats wrong?
Re: Random player! -
armyoftwo - 22.03.2011
Mike_Garber, your function might be buggy. Why? For example you have 10 players in your server with id's 0, 1, 3, 5, 6, 9, 10, 12, 13, 14. Random function might pick Non existing ID, for example 4.
Foreach include has an function with what you can pick random player with no bugs( search for it )
Re: Random player! -
Jefff - 22.03.2011
pawn Код:
stock FirstInfected()
{
new array[MAX_PLAYERS],count; // replace MAX_PLAYERS to Your server slots
for(new x=0; x < MAX_PLAYERS; x++) { // up
if(IsPlayerConnected(x) && !IsPlayerInfected(x)) { // replace 'IsPlayerInfected' to Your own array / function
array[count] = x;
count++;
}
}
return array[random(count)];
}
// Then do:
new randomid = FirstInfected();
Re: Random player! -
Mike Garber - 22.03.2011
Quote:
Originally Posted by armyoftwo
Mike_Garber, your function might be buggy. Why? For example you have 10 players in your server with id's 0, 1, 3, 5, 6, 9, 10, 12, 13, 14. Random function might pick Non existing ID, for example 4.
Foreach include has an function with what you can pick random player with no bugs( search for it )
|
It's not my function, but yeah I guess... And i know about foreach but he might not have it... But
my code can easily be rewritten to not pick an invalid ID.
pawn Код:
GetPlayersOnServer() {
new count;
for(new x=0; x< MAX_PLAYERS; x++) { //x = MAX_PLAYERS
if(IsPlayerConnected(x))) {
count++;
}
}
return count;
}
// Then do;
randomplayer()
{
new randomid = random(GetPlayersOnServer());
if(!IsPlayerConnected(randomid)){ return randomplayer(); }
return randomid;
}
//And now you can do a function like;
infectplayer(randomplayer);
This should work.
Re: Random player! -
GNGification - 27.03.2011
Sorry but it didnt...
Stand.pwn(955) : error 029: invalid expression, assumed zero
if(IsPlayerConnected(x))) { is the error line
I guess you meant if(IsPlayerConnected(x)) ?