SA-MP Forums Archive
[Tutorial] Making a simple /clearchat and /clearallchat command - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] Making a simple /clearchat and /clearallchat command (/showthread.php?tid=535343)



Making a simple /clearchat and /clearallchat command - DarkSkull - 03.09.2014

Introduction

As the the title says, in this tutorial, will be making a /clearchat and /clearallchat command.

/clearchat command can be used by the player to clear his/her chat.
/clearallchat command can be used by an admin to clear all players chat.

So. Let's Get Started.

Making /clearchat Command

So, we will be using the callback OnPlayerCommandText, as it is called when the player enters a command.

pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
    if (strcmp("/clearchat", cmdtext, true, 10) == 0)
    {
        // Do something here
        return 1;
    }
    return 0;
}
So, making this command is really simple. We will use a for loop to send blank messages a hundred times, which will basically remove all the messages by adding a lot of blanks. So,

pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
    if (strcmp("/clearchat", cmdtext, true, 10) == 0)
    {
        for (new i = 0; i <= 100; i++) {
            SendClientMessage(playerid, -1, " ");
        }
        return 1;
    }
    return 0;
}
Here, we are creating a new variable 'i', whose value will be zero, checking if 'i' is less than or equal to 100, if it is, then it will run the block and increase the value of 'i', and it will continue till the value of i becomes 100. When it becomes 100, the loop will stop. Thats how a for loop works.

Congratulations, You have created a /clearchat command.

Making /clearallchat Command

This is basically the same concept but, it can only be used if the player is an admin. So,

pawn Код:
if (strcmp("/clearallchat", cmdtext, true, 10) == 0)
    {
        if (!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xFF0000FF, "Only admins can use that command");
        for (new i; i <= 100; i++) {
            SendClientMessageToAll(-1, " ");
        }
        return 1;
    }
Here we are checking if the player is admin by using the function IsPlayerAdmin(). It is a function which returns true or false depending on whether the player is RCON admin or not. We use the Exclamation Mark (!) to check if the function has returned false. If the function returns false , i.e., If the player is not admin, then send him/her a message saying "Only admins can use that command". If the function returns true, i.e, If the player is an admin, we run the loop, and send the blank message to all instead of just sending to a player. So everyone's chat will be cleared.

Congratulations, You have created a /clearallchat command.

Final Code.

pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
    if (strcmp("/clearchat", cmdtext, true, 10) == 0)
    {
        for (new i; i <= 100; i++) {
            SendClientMessage(playerid, -1, " ");
        }
        return 1;
    }
   
    if (strcmp("/clearallchat", cmdtext, true, 10) == 0)
    {
        if (!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xFF0000FF, "Only admins can use that command");
        for (new i; i <= 100; i++) {
            SendClientMessageToAll(-1, " ");
        }
        return 1;
    }
    return 0;
}
And that's it, Please read and understand instead of just copy/paste.

Hope you like it.



Re: Making a simple /clearchat and /clearallchat command - Stinged - 03.09.2014

To be honest, you're not really explaining the functions.


Re: Making a simple /clearchat and /clearallchat command - DarkSkull - 03.09.2014

Functions? I din't get you.


Re: Making a simple /clearchat and /clearallchat command - PMH - 03.09.2014

don't know about the tutorial, but u explained loop system really well.. i never understood it, just used to copy paste it and use it but now i know how it really works...


Re: Making a simple /clearchat and /clearallchat command - DarkSkull - 03.09.2014

Quote:
Originally Posted by PMH
Посмотреть сообщение
don't know about the tutorial, but u explained loop system really well.. i never understood it, just used to copy paste it and use it but now i know how it really works...
Hehe, Thanks


Re: Making a simple /clearchat and /clearallchat command - Jack_Leslie - 03.09.2014

This isn't really a tutorial. It's more just giving a simple code. Also, if you don't "get" functions then you shouldn't be writing a tutorial. I know you're just trying, but try and learn more, so you can write a better tutorial, about something worth writing a tutorial about.


Re: Making a simple /clearchat and /clearallchat command - DarkSkull - 03.09.2014

Quote:
Originally Posted by Jack_Leslie
Посмотреть сообщение
This isn't really a tutorial. It's more just giving a simple code. Also, if you don't "get" functions then you shouldn't be writing a tutorial. I know you're just trying, but try and learn more, so you can write a better tutorial, about something worth writing a tutorial about.
Well.. I was just asking which function i did'nt explain..


Re: Making a simple /clearchat and /clearallchat command - DarkSkull - 04.09.2014

Quote:
Originally Posted by Stinged
Посмотреть сообщение
To be honest, you're not really explaining the functions.
Sorry, I forgot to explain IsPlayerAdmin.. Right now am on mobile...i will update as soon as i get on the computer. My Baad


Re: Making a simple /clearchat and /clearallchat command - MrReBzz - 04.09.2014

Nice One.. +REP


Re: Making a simple /clearchat and /clearallchat command - Flake. - 04.09.2014

Pretty much just
pawn Код:
if (strcmp("/clearchat", cmdtext, true, 10) == 0)
    {
        for(new i = 0; i < 50; i++) SendClientMessageToAll(COLOR_WHITE," ");
        return 1;
    }