Loops through players
#1

Im trying to learn how to loop and stuff like that. And i can't get this to work!

pawn Код:
stock GetName( playerid )
{
    new Name[ MAX_PLAYER_NAME ];
   
    if( IsPlayerConnected( playerid ) )
    {
        GetPlayerName( playerid, Name, sizeof( Name ) );
    }
    else
    {
        Name = "Disconnected/Nothing";
    }
   
    return Name;
}
pawn Код:
forward GAPoint( playerid );
public GAPoint( playerid )
{
    new string[ 128 ];
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        format( string, sizeof( string ), "** %s Won! **", GetName( i ) );
        SendClientMessageToAll(YELLOW, string);
    }
}
Ingame it just says "** Disconnected/Nothing Won! **" and spams it.
Reply
#2

It's because your not checking if the player is online, you can't work with offline players.

Try this:

pawn Код:
forward GAPoint( playerid );
public GAPoint( playerid )
{
    new string[ 128 ];
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i))
        {
          format( string, sizeof( string ), "** %s Won! **", GetName( i ) );
          SendClientMessageToAll(YELLOW, string);
        }
    }
}
Also i think this is pointless, because players will 'win' purely by being just online (and it will spam the chat).
Reply
#3

Quote:
Originally Posted by Whizion
Посмотреть сообщение
It's because your not checking if the player is online, you can't work with offline players.

Try this:

pawn Код:
forward GAPoint( playerid );
public GAPoint( playerid )
{
    new string[ 128 ];
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i))
        {
          format( string, sizeof( string ), "** %s Won! **", GetName( i ) );
          SendClientMessageToAll(YELLOW, string);
        }
    }
}
Also i think this is pointless, because players will 'win' purely by being just online (and it will spam the chat).
Yea well that is just a small code i wrote i wouldn't paste the whole code its long and it would be pointless. But thanks for the reply i will try with that connected .
Reply
#4

Okay it kinda works. As i said im new to this loop thing. But is "i" every players? and is "playerid" the player or?
because it works but it sends the clientmessage with all the players names and i only want the player that does this
to be the name in the clientmessage. (That "GaPoint" is a timer)
Reply
#5

you need to imagine "i" as a variable.
the playerID is a number...
so... the "playerid" is a pre-set number (the number of the player that uses the command or that you have entered, etc.... = the ID of a specific player)...
the "for" loop will run again and again as long as the limit "i < MAX_PLAYERS" is reached..
you have this code:
pawn Код:
for(new i=0; i < MAX_PLAYERS; i++)
the "new i=0" creates the variable "i" and sets the value that is connected to it to "0" (so it would be the same as the player that has the ID 0)...
i < MAX_PLAYERS is the limit... The loop will repeat itself until the MAX_PLAYERS value is reached (I think it's 500)
i++ makes the "i value" increasing by 1 each time the loop repeats... so for the first time i is 0.. for the second it's 1, the third 2, etc...
So "i" has another value each time the loop runs, where playerid always stays the same.
If you want now enter "playerd" somewhere into the loop - example SendClientMessage(playerid......) - it will send the message to the player everytime the loop runs and gets to this point, but if you enter "i" instead - SendClientMessage(i, .........) - it will send the message to the "i" ID each time. As this ID is increasing by 1 in each round, it will always get another player.
If you have bigger codes within the loop it is stupid to make it do the defined thing for each ID ("i") untill the MAX_PLAYER limit is reached, as it doesn't have any effact to the "not connected" players.
Therefore you should enter "IsPlayerConnected(i)" before using the it, so it will only do it for players that are even connected...

however:
If the "playerid" variable is not defined yet (for example on a "public" that you created yourself by forwarding or on a "stock" that doesn't have the (playerid) variable, then you can use playerid the same way as "i"
e.g.
pawn Код:
//this will send a message to all connected players, as the "playerid" is not defined before and does the same as the "i" variable in this case
forward SendNoobMsg(text[]);
public SendNoobMsg(text[])
{
  for(new playerid=0; playerid < MAX_PLAYERS; playerid++)
  {
    if(IsPlayerConnected(playerid))
    {
      SendClientMessage(playerid, COLOR, text);
    }
  }
  return 1;
}
I hope that helped lol...
Reply
#6

Quote:
Originally Posted by Sascha
Посмотреть сообщение
hes reply..
Thank you so much. That helped me allot .
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)