total connected players
#1

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..
Reply
#2

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
Reply
#3

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.
Reply
#4

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)
Reply
#5

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?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)