SA-MP Forums Archive
Get all ids of connected players? - 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)
+--- Thread: Get all ids of connected players? (/showthread.php?tid=389026)



Get all ids of connected players? - Azazelo - 31.10.2012

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
}




AW: Get all ids of connected players? - Skimmer - 31.10.2012

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


Re: Get all ids of connected players? - Azazelo - 31.10.2012

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


Re: Get all ids of connected players? - Jefff - 31.10.2012

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;
}



AW: Get all ids of connected players? - Skimmer - 31.10.2012

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


Re: Get all ids of connected players? - iggy1 - 31.10.2012

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.


Re: Get all ids of connected players? - Azazelo - 31.10.2012

Thank you all for help.