SA-MP Forums Archive
total 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: total connected players (/showthread.php?tid=329488)



total connected players - rVar - 28.03.2012

What is wrong with this code?
Код:
// Under ongamemodeinit
_mPlayers = GetMaxPlayers( );
 SetTimer( "oPlayers", rSec2, 1 );

// defines
new _
mPlayers,
rTotalOn
;

// function
forward oPlayers( );
public oPlayers( )
{
	rTotalOn = 0;
    for( new i=0; i<MAX_PLAYERS; i++ )
	{
        if( IsPlayerConnected( i ) )
		{
            if( rTotalOn == 0 )
			{
                rTotalOn++;
            }
   		}
   	}
    return 1;
}
When i have on server more than 1 player it shows only 1/150 players..


Re: total connected players - ikkentim - 28.03.2012

pawn Код:
// Under ongamemodeinit
_mPlayers = GetMaxPlayers( );
 SetTimer( "oPlayers", rSec2, 1 );

// defines
new _
mPlayers,
rTotalOn
;

// function
forward oPlayers( );
public oPlayers( )
{
    rTotalOn = 0;
    for( new i=0; i<MAX_PLAYERS; i++ )
    {
        if( IsPlayerConnected( i ) )
        {
            //if( rTotalOn == 0 ) remove this line
        //  { remove this line
                rTotalOn++;
            //} remove this line
        }
    }
    return 1;
}
remove the lines i //'ed


I do though, strongly suggest doing this:
pawn Код:
//on top of your code
new onlineplayers;
// under onPlayerConnected
onlineplayers++;
// under onPlayerDisconnect
onlineplayers--;
when you then need to see how many players are online, look for onlineplayers' value


Re: total connected players - rVar - 28.03.2012

Right now before looking again on this post i've looked more carefully on that code and delete those lines exactly how you do it, i hope it works.


Re: total connected players - DRIFT_HUNTER - 28.03.2012

First way
pawn Код:
new OnlinePlayersCount;//On top
public OnPlayerConnect(playerid)
{
    if(!IsPlayerNPC(playerid)) OnlinePlayersCount++;
    return 1;
}

public OnPlayerDisconnect(playerid)
{
    if(!IsPlayerNPC(playerid)) OnlinePlayersCount--;
    return 1;
}
Second way
pawn Код:
stock OnlinePlayersCount()
{
    new count = 0;
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(!IsPlayerNPC(playerid)) count++;
    }
    return count;
}
Now you can use OnlinePlayersCount and it will return you integer (Number of players on server)


Re: total connected players - admantis - 29.03.2012

everytime you need to request the connected players, you can make a new variable and use foreach, and under that loop increase the value of your variable. why using a timer?