16.08.2015, 15:47
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:
I'm sure you all know why we need to add this in every script we do.(right? :P )
Secondly,add this:
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:
Lets break the code down.
Remember when we started scripting,we had set the variable's value to ( new onlineplayers=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.
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.
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.
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:
'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.
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>
Secondly,add this:
pawn Код:
new onlineplayers=0;
'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);
pawn Код:
onlineplayers++;
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];
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);
'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)"
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);
'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.
Rep me if you like it!