[Tutorial] Creating a group chat - Annotated & explained
#1

TUTORIAL - Creating a group chat

Hi, so in this tutorial I will be explaining how to create group chats that you could use for faction chats, admin chats, helper chats, the list goes on. Here's how you do it, I will be using ZCMD and sscanf for this, you'd be able to download it from ******. You'll need some basic knowledge of scripting before you do this, the basic knowledge is basically knowing what variables are.

Step 1: You will need to make the command structure, so let's start off with this, I will be making an RCON admin chat in this tutorial, but you can do any type of chat.

pawn Code:
CMD:rconchat(playerid, params[]) // this is called when someone in the game uses the command /rconchat
{ // opens the code
    return 1; // returns a value of true
} // closes the code
Step 2: You'd want to make sure now, that the person who is using the command is an RCON admin who is logged on too, so let's try this:

pawn Code:
CMD:rconchat(playerid, params[])
{
    if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xFFFFFFFF, "You are not an RCON admin.");
/* The explanation mark before the IsPlayerAdmin means NOT. So basically, if(IsPlayerNotAnAdmin.
And the (playerid) is the person who uses the command. The return of the SendClientMessage is
sending the message with the color of 0xFFFFFFFF saying that you're not an RCON admin.*/

    return 1;
}
Step 3: We need to declare the variables for this, so let's make some public variables to the command:

pawn Code:
CMD:rconchat(playerid, params[])
{
    if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xFFFFFFFF, "You are not an RCON admin.");
    new ChatText[100], string[130], AdminName[MAX_PLAYER_NAME];
/* This is declaring 3 new public variables, the name of the admin with AdminName with an array
of the MAX_PLAYER_NAME and the ChatText, which means how much that person can type.
The string[130] adds up so it means how much it is altogether, because we can't use
variables with SendClientMessage, you'll see and understand what I mean after. */

    return 1;
}
Step 4: We need to declare the parameters, by using sscanf which you can find on the SA-MP forums:

pawn Code:
CMD:rconchat(playerid, params[])
{
    if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xFFFFFFFF, "You are not an RCON admin.");
    new ChatText[100], string[130], AdminName[MAX_PLAYER_NAME];
    if(sscanf(params, "s[100]", ChatText)) return SendClientMessage(playerid, 0xFFFFFFFF, "Correct usage: /rconchat [text]");
/* This line is basically telling the script that if the person does not type anything,
then it will return that specific message of the "Correct usage: ...". The s[100] declares
a string that we'll be using, and the 100 is the character count as in the ChatText.
More information about sscanf can be found on the sscanf topic, just ****** it. */

    return 1;
}
Step 5: After we've made sure everything is in order with the latest command that you can see above this particular text, we need to get the player's name:

pawn Code:
CMD:rconchat(playerid, params[])
{
    if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xFFFFFFFF, "You are not an RCON admin.");
    new ChatText[100], string[130], AdminName[MAX_PLAYER_NAME];
    if(sscanf(params, "s[100]", ChatText)) return SendClientMessage(playerid, 0xFFFFFFFF, "Correct usage: /rconchat [text]");
    GetPlayerName(playerid, AdminName, sizeof(AdminName));
/* This will simply get the playerid's name, remember, the playerid is the person
who send the command. The AdminName is the variable that we declared above
with the array of MAX_PLAYER_NAME, and the sizeof(AdminName) means that
the size of the AdminName is MAX_PLAYER_NAME which is already defined
in a_samp.inc */

    return 1;
}
Step 6: We will now loop all the online players and check if they're RCON admins:

pawn Code:
CMD:rconchat(playerid, params[])
{
    if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xFFFFFFFF, "You are not an RCON admin.");
    new ChatText[100], string[130], AdminName[MAX_PLAYER_NAME];
    if(sscanf(params, "s[100]", ChatText)) return SendClientMessage(playerid, 0xFFFFFFFF, "Correct usage: /rconchat [text]");
    GetPlayerName(playerid, AdminName, sizeof(AdminName));
    for(new i = 0; i < MAX_PLAYERS; i ++)
    {
        if(IsPlayerConnected(i))
        {
            if(IsPlayerAdmin(i))
            {

            }
        }
    }
/* This code here is basically telling the script to loop all the connected players and
check if they're logged in as RCON admins, and if they are, it will do Step 7 */

    return 1;
}
Step 7: After that, we need to format the string that we used earlier, and send the message to all the RCON'ly logged in admins:

pawn Code:
CMD:rconchat(playerid, params[])
{
    if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xFFFFFFFF, "You are not an RCON admin.");
    new ChatText[100], string[130], AdminName[MAX_PLAYER_NAME];
    if(sscanf(params, "s[100]", ChatText)) return SendClientMessage(playerid, 0xFFFFFFFF, "Correct usage: /rconchat [text]");
    GetPlayerName(playerid, AdminName, sizeof(AdminName));
    for(new i = 0; i < MAX_PLAYERS; i ++)
    {
        if(IsPlayerConnected(i))
        {
            if(IsPlayerAdmin(i))
            {
                format(string, sizeof(string), "RCON CHAT: %s said: %s", AdminName, ChatText);
                SendClientMessage(i, 0xFFFFFFFF, string);
/* This code here will send all the RCON'ly online admins the text
that we formatted in the string, we use a string because SendClientMessage
simply won't support variables, i.e. the variables AdminName and the ChatText
that we used. */

            }
        }
    }
    return 1;
}
That's all! Now, if you want to make your own commands, then you can just use this and change the variables to how you want it. I hope you understood how to make group chats now.
Reply
#2

It's not working when I get to step 3...
Reply
#3

pawn Code:
if(sscanf(params, "s[100]", ChatText)) return SendClientMessage(playerid, 0xFFFFFFFF, "Correct usage: /rconchat [text]");
This cmd doesn't need sscanf

This forum requires that you wait 240 seconds between posts. Please try again in 1 seconds.
Reply
#4

really good tut thank you so much where can i learne more about the loop?
Reply
#5

Simple and easy, good for new Scripters!
Reply
#6

Quote:
Originally Posted by Another1
View Post
really good tut thank you so much where can i learne more about the loop?
Here Try ****** for results
Reply
#7

well done dani C:, :3 i will use it in my gm (in the next update)
thank you
Reply
#8

Thanks. :P.
Reply
#9

May I improve few things?

Declare variables when they're about to be used. In a case that it will return an error about the usage, we simply declared those arrays with no reason!

Commands such as this one with only text as parameter (1 parameter only) can be used with params (and isnull for the usage (error message)). By that, we get rid of an array with size of 100!

The normal method of for loop is slow in case not many players are online. It's much better to use foreach so you gain much speed!

The last one is about format. You used it inside the for loop so it will basically formats the same message over and over again. It's better to post it before the loop!

So the final result:
pawn Code:
CMD:rconchat(playerid, params[])
{
    if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xFFFFFFFF, "You are not an RCON admin.");
    if(isnull(params)) return SendClientMessage(playerid, 0xFFFFFFFF, "Correct usage: /rconchat [text]");
    new string[144], AdminName[MAX_PLAYER_NAME];
    GetPlayerName(playerid, AdminName, sizeof(AdminName));
    format(string, sizeof(string), "RCON CHAT: %s said: %s", AdminName, params);
    foreach(new i : Player) if(IsPlayerAdmin(i)) SendClientMessage(i, 0xFFFFFFFF, string);
    return 1;
}
PS: Good tutorial!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)