How to check if anyone is online?
#1

This is the code:

PHP код:
public OnPlayerSpawn(playerid
{
        if(
IsPlayerConnected(0)) // <-- This
        
{
            
SetTimer("startgm",100,false);
         }
         else
         {
            return 
0;
         }

Instead of "ID0", it will check if any play is online, it doesn't matter who. Just if any player is connected it will work. Can anyone help me with it?
Reply
#2

Use this function
pawn Код:
//on top of the script
bool:IsAnyPlayerOnline()
{
    for(new i=0;i<MAX_PLAYERS;i++)if(IsPlayerConnected(i))return true;
    return false;
}
//_________________________________________

//example how to use it
public OnPlayerSpawn(playerid)  
{
        if(IsAnyPlayerOnline())
        {
            SetTimer("startgm",100,false);
         }
         else
         {
            return 0;
         }
}
Reply
#3

pawn Код:
stock IsAnyPlayerOnline()
{
     for(new i=0;i<MAX_PLAYERS;i++)
     {
           if(IsPlayerConnected(i)) return 1;
     }
     return 0;
}
IsAnyPlayerOnline will return 1 if there is a player connected and 0 if none is connected.

EDIT: User above was quicker.
Reply
#4

if you want to check if someone is in the server or not you can just use a loop to check if anyone is online:

pawn Код:
new iCountPlayers = 0;

for( new f = 0; f < MAX_PLAYERS; f++ )
{
    if( IsPlayerConnected( f ) )
    {
        iCountPlayers++;
    }
}

if( iCountPlayers > 0 )
{
    // your code
}
or alternatively

pawn Код:
stock IsAnyPlayerOnline( )
{
    new iCountPlayers = 0;

    for( new f = 0; f < MAX_PLAYERS; f++ )
    {
        if( IsPlayerConnected( f ) )
        {
            iCountPlayers++;
        }
    }
   
    if( iCountPlayers > 0 )
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)