Get all ids of connected players?
#1

Best(fastest) way to get id of all player connected in game?

Quote:

stock ConnectedPlayersIDs()
{
new a = MAX_PLAYERS,counter;
do
{

if(IsPlayerConnected(counter)) ; // best way to store
counter++;

}
while(a<=counter);

return ;// what array
}

Reply
#2

Add this top of the script

pawn Код:
new PlayerID[MAX_PLAYERS];
pawn Код:
stock ConnectedPlayersIDs()
{
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i))
        {
            PlayerID[i] = i;
        }
    }
    return 1;
}
You can use PlayerID[playerid] to get anyones ID
Reply
#3

Thank and when i search for players in game i do same loop
And if for example PlayerID 5 not used but 6 7 8 used i got ther is PlayerID[5] = 6
Right
Reply
#4

pawn Код:
new ConnectedID[MAX_PLAYERS];

stock ConnectedPlayersIDs()
{
    new Counter;

    for(new i,g = GetMaxPlayers(); i < g; i++)
        if(IsPlayerConnected(i))
            ConnectedID[Counter++] = i;

    return Counter;
}
Reply
#5

My question, why are you using this stock ?
Create always the for-loop. Here's an example.
I'll give everyone $500 cash!

pawn Код:
for(new i = 0; i < MAX_PLAYERS; i++)
{
    if(IsPlayerConnected(i))
    {
        GivePlayerMoney(i, 500);
        SendClientMessage(i, -1, "An Admin has sent you $500.");
    }
}
So, i use i instead of playerid
Reply
#6

The best, easiest and fastest would be using foreach.
pawn Код:
foreach(new playerid, Player)
{
    //this loops through only connected players
   
    //either store in an array like above or just use "playerid" (or any other identifier you choose)
}
Like the comment says it will only loop through connected players never more.
Reply
#7

Thank you all for help.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)