[Tutorial] How to display current online players(OnPlayerConnect)
#1

INTRODUCTION

Hi,in this you tutorial you will be able to learn:
1.How you can display current number of online players when a player connects to the server.

How does it look like in-game?


Let's get started

Step 1:

Go open up Pawno and then press File --> New.
You'll see a bunch of codes just select the entire thing and press the delete button.

Step 2:

Now on top of your script you have to add two things.
First ,add this:
pawn Код:
#include<a_samp>
I'm sure you all know why we need to add this in every script we do.(right? :P )

Secondly,add this:
pawn Код:
new onlineplayers=0;
This is a variable.
'new' is the variable and 'onlineplayers' is the variable name.You can change the name of the variable to whatever you want.
The reason we have added this on to the top of our script is because its a global variable i.e we can use this under any function.

'onlineplayers=0' ,this means that we are assigning the variable's value as 0 at the start of the script(it will change later on).

Step 3:

Now we are going to script under 'public OnPlayerConnect(playerid)'.
So under OnPlayerConnect,add this:
pawn Код:
onlineplayers++;
new str[128];
format(str,sizeof(str),"SERVER STATUS: %i players are currently online.",onlineplayers);
SendClientMessageToAll(0xED5F07FF,str);
Lets break the code down.
pawn Код:
onlineplayers++;
Remember when we started scripting,we had set the variable's value to ( new on‌lineplayers=0; ).
Well what this code does is that it just adds a value of 1 when a player joins your server.

Further explanation:
'++' is an increment operator i.e it increases the value of any variable by 1/adds 1 to it.
So when a player joins your server,a player/1 player extra has just connected to the server.So the total number of players increases by 1.(Its just commonsense.)

Note:-
You can also use 'onlineplayers+=1' instead of 'onlineplayers++' .It does the SAME thing.

pawn Код:
new str[128];
Here we are creating another variable and this time the variable is called str or string.
The [128] here is a value i.e it basically tells how many letters/characters you can fit inside that variable.The max is 128.

pawn Код:
format(str,sizeof(str),"SERVER STATUS: %i players are currently online.",onlineplayers);
What this code basically does is it formats/converts(in easier words) the integer value(onlineplayers++,remember this adds 1 to the variable and 1 is an INTEGER)to a string a value.

'str' - is simply the name of the variable we had set above.(new str[128])

'sizeof(str) - We know that the value of the variable/the number of letters/characters the variable can hold is 128.([new str[128]).So 'sizeof' basically gets that value of the string/variable. In this case the value of the string/variable is 128.

pawn Код:
"SERVER STATUS: %i players are currently online.,onlineplayers)"
In the output picture we saw that in place of the '%i' there is the number of players online.
So you basically get what the %i does or means.

Further explanation:
%i ,%d,%s etc are format specifiers used in such cases.
The 'i' stands for integer.
So in this case,the value/number of online players is in the variable 'onlineplayers' ,that value with the help of a format specifier (%i) gets displayed as a message in the server chat.

Vist for more info:
https://sampwiki.blast.hk/wiki/Format

Step 4:
pawn Код:
SendClientMessageToAll(0xED5F07FF,str);
'SendClientMessageToAll' simply just sends the message to all the players online.
'0xED5F07FF' is the color in which the message should be displayed to other players.
'str' this from where the code gets the message which has to be displayed to all the players.
Remember str is a variable and a variable has a value.In this case its the message (SERVER STATUS ....).
So 'SendClientMessageToAll' just gets the message/value from 'str' and sends a message to all the players.


Note(s):
1.If you want more colors:
Go to www.colorpicker.com
Choose any color you want.
Paste it in PAWN and add '0x' in front of it and 'FF' at the end.
Thanks for reading!Hope this helped!
Rep me if you like it!
Reply
#2

Quote:

'new' is the variable

No, "new" is keyword initialiser.
It allows among other 3 (const, static, stock) to tell to the compiler that the word that follows is a variable.
Reply
#3

Also, you can avoid this by simply pressing TAB in game. I appreciate your initiative though.
Reply
#4

First of all you don't need to do
PHP код:
new onlineplayers=0
If the variable has not any value set, it automatically sets it to 0 so
PHP код:
new onlineplayers
is enough

and

what Dutheil said

and

Your system won't work, if any player leaves it won't remove 1 to the count, so it will just keep adding when a player join, but not remove when a player leaves!

PHP код:
public OnPlayerDisconnect(playeridreason)
{
    
onlineplayers--;
    new 
str[128];
    
format(str,sizeof(str),"SERVER STATUS: %i players are currently online.",onlineplayers);
    
SendClientMessageToAll(0xED5F07FF,str);
    return 
1;

Reply
#5

You can make a simple loop to check connected players ... will be easiest and short to write :

PHP код:
#include <a_samp>
#if defined MAX_PLAYERS // Check if MAX_PLAYERS it's defined
  #undef MAX_PLAYERS // If yes , we will undefine it
#endif // if we have an #if , we need #endif ..
#define MAX_PLAYERS 50 // Define server slots | Modify there .
public OnPlayerConnect(playerid// Public OnPlayerConnect
// Open bracket
    
new countString[46]; // We create an initialiser (count) and a string with the cellsize 46 (String)
    
for(new ii<MAX_PLAYERSi++) if(IsPlayerConnected(i)) count++; // Make a loop across all server ids (slots ...) , check if any it's connected . If a player it's connected we will give count += 1 (Or count++)
    // count = players connected .
    
format(Stringsizeof(String), "SERVER INFO:There are %i players online."count); // Format our string with the text from "  "
    
SendClientMessage(playerid, -1String); // Print the message to player who connected in white color .
// Close the bracket ~~~~ Much simplest , i think 
Reply
#6

I'd like to point few of the things:

- You don't have to initialize a variable with 0, it should be 0 by default.
- You don't need to use OnPlayerConnect() or OnPlayerDisconnect() to increment or decrement any specific variable, you could simply check current players with a simple for() loop.
- You don't need "128" array length, just use 50 which should be more than enough.
- If this is for a player command, you're using SendClientMessageToAll() which will send the message to "all" players instead of player, use SendClientMessage() instead.
Reply
#7

Quote:
Originally Posted by iZN
Посмотреть сообщение
- You don't need to use OnPlayerConnect() or OnPlayerDisconnect() to increment or decrement any specific variable, you could simply check current players with a simple for() loop.
It's probably more efficient to use the variable, still. On another note, if you happen to use foreach you may simply use Iter_Count instead.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)