[Tutorial] Making a mute system
#1

Introduction:

This is a mute system, where RCON logged in admins can use these commands, but you can simply change the commands to whatever you want.

Downloads:

sscanf2
ZCMD

Tutorial: The top of your script:

At the top of your script, under #include <a_samp> you need to add the following:

pawn Code:
#include <sscanf2> // include the sscanf file
#include <zcmd> // include the zcmd file

new Mute[MAX_PLAYERS]; // global variable to check if a player is muted

forward SecondTimer(); // timer that we will be using that will tick second-by-second
Tutorial: OnGameModeInit:

Here, you will need to start the timer that will tick every 1 second at a repeating fashion.

pawn Code:
public OnGameModeInit()
{
    SetTimer("SecondTimer", 1000, true); // this will initiate the timer
    return 1; // don't put this line if you've already got return 1 at the bottom of your OnGameModeInit
}
Tutorial: Our custom callback:

Now, we forwarded our SecondTimer, so now it's time to make a callback out of it, as follows:

pawn Code:
public SecondTimer()
{
    for(new i = 0; i < MAX_PLAYERS; i ++) // loops all the players
    {
        if(IsPlayerConnected(i)) // will check if the player is connected
        {
            if(Mute[i] >= 1) // will check if their mute time is equal or above to 1
            {
                Mute[i] --; // if it is, then it will minus the mute time by 1 second
            }
        }
    }
    return 1;
}
Ok, so that's our callback done that will tick automatically every second.

Tutorial: OnPlayerDisconnect:

If a player disconnects whilst having a mute, then it will do the following:

pawn Code:
public OnPlayerDisconnect(playerid, reason)
{
    if(Mute[playerid] >= 1) // will check if the player was muted
    {
        new mutename[MAX_PLAYER_NAME], string[100]; // creates a few new public variables
        GetPlayerName(playerid, mutename, sizeof(mutename)); // retrieve the player name
        format(string, sizeof(string), "%s just left the game with a remaining mute time of %d seconds", mutename, Mute[playerid]); // will set the string that we'll use
        for(new i = 0; i <= MAX_PLAYERS; i ++) // again, loops all the players
        {
            if(IsPlayerConnected(i) && IsPlayerAdmin(i)) // check if the player is connected and if they're an RCON admin
            {
                SendClientMessage(i, 0xAFAFAFFF, string); // send the message to the player
            }
        }
    }
    Mute[playerid] = 0; // this will reset the mute time, so the next person that logs in with the same ID won't suffer a mute
    return 1; // don't add this line if you've already got it at the bottom of your OnPlayerDisconnect
}
So, now, that will send a message to all the RCON admins online telling them the name of the person and that they quit whilst they had an outstanding mute time of more than 0 seconds.

Tutorial: OnPlayerText:

For this part of the tutorial, we need to get the player to stop talking if he's muted already, so this is what we'll do:

pawn Code:
OnPlayerText(playerid, text[])
{
    if(Mute[playerid] >= 1) // check if the player is muted
    {
        new string[50]; // creates a new variable
        format(string, sizeof(string), "You're muted for another %d seconds.", Mute[playerid]); // sets the string
        SendClientMessage(playerid, 0xFFFFFFFF, string); // sends the message
        return 0; // this is crucial, by returning 0 here it will stop the player from being able to talk
    }
    return 1; // don't put this here if you've already got it at the bottom of your OnPlayerText.
}
So, here, if the player tries to talk, it will stop the player from talking and send them a message stating that they can't talk because they're muted and how much longer they're muted for.

Tutorial: Command /mute:

Let's start creating the commands, these will be available to RCON admins, but you can change them to your own variables:

pawn Code:
CMD:mute(playerid, params[])
{
    if(!IsPlayerAdmin(playerid)) return 0; // returns "SERVER: Unknown command." if the player is not an RCON admin
    new giveplayerid, reason[50], string[120], minutes, pname1[MAX_PLAYER_NAME], pname2[MAX_PLAYER_NAME]; // declares a few new variables
    if(sscanf(params, "uds[50]", giveplayerid, minutes, reason)) return SendClientMessage(playerid, 0xFFFFFFFF, "Correct usage: /mute [playerid/name] [time in minutes] [reason]"); // returns this message if the player typed an incorrect syntax
    if(giveplayerid == INVALID_PLAYER_ID) return SendClientMessage(playerid, 0xFFFFFFFF, "Player not connected."); // returns this message if the receiver player is not connected
    GetPlayerName(playerid, pname1, sizeof(pname1)); // get the sender name
    GetPlayerName(giveplayerid, pname2, sizeof(pname2)); // get the receiver name
    format(string, sizeof(string), "%s (%d) was muted for %d minutes by admin %s (%d), reason: %s", pname2, giveplayerid, minutes, pname1, playerid, reason); // sets the string
    SendClientMessageToAll(0xFFFFFFFF, string); // sends a global message
    new calc = minutes*60; // calculates the seconds
    Mute[giveplayerid] = calc; // sets the mute time to the calculated value
    GameTextForPlayer(giveplayerid, "~r~muted~n~~w~check the chat", 5000, 6); // 5 second game text for the receiver that got the mute informing him to check the chat for information about the mute
    return 1;
}
So, now, we have the /mute command.

Tutorial: Command /unmute:

Let's make an unmute command, just incase you might need it:

pawn Code:
CMD:unmute(playerid, params[])
{

    if(!IsPlayerAdmin(playerid)) return 0;
    new giveplayerid, reason[50], string[120]; // variables
    if(sscanf(params, "us[50]", giveplayerid, reason)) return SendClientMessage(playerid, 0xFFFFFFFF, "{FFFF00}Syntax: /unmute [playerid/name] [reason]"); // syntax check
    if(giveplayerid == INVALID_PLAYER_ID) return SendClientMessage(playerid, 0xFFFFFFFF, "{FFFF00}Player not connected."); // player not connected
    new pname1[MAX_PLAYER_NAME], pname2[MAX_PLAYER_NAME]; // more variables
    GetPlayerName(playerid, pname1, sizeof(pname1)); // gets the senders name
    GetPlayerName(giveplayerid, pname2, sizeof(pname2)); // gets the receivers name
    format(string, sizeof(string), "%s (%d) was unmuted by %s (%d), reason: %s", pname2, giveplayerid, pname1, playerid, reason); // sets the string
    SendClientMessageToAll(0xFFFFFFFF, string); // sends everyone the message that was string'd
    Mute[giveplayerid] = 0; // sets the mute of the other player to 0
    GameTextForPlayer(giveplayerid, "~g~unmuted~n~~w~check the chat", 5000, 6); // game text for the other player informing them that his mute has been taken off him
    return 1;
}
That was the unmute command for you.

Tutorial: Command /mymute:

This is just gonna be a simple command telling the player how much time he has to wait until their auto-unmute:

pawn Code:
CMD:mymute(playerid, params[])
{
    if(Mute[playerid] <= 0) return SendClientMessage(playerid, 0xFFFFFFFF, "{FFFF00}You're not muted at the moment."); // checks if the player is muted, if not, it will return that message
    new string[50]; // new variable
    SendClientMessage(playerid, 0xFFFFFFFF, "|__________ My Mute __________|"); // sends a base message
    format(string, sizeof(string), "%d seconds remain from your mute.", Mute[playerid]); // sets the string
    SendClientMessage(playerid, 0xFFFFFFFF, string); // sends the string'd message
    return 1;
}
That was just an addition if you needed it, or wanted it for any reason.

Tutorial: Command /mutelist:

This command would show all the players online who are muted, you can do this as follows:

pawn Code:
CMD:mutelist(playerid, params[])
{
    new iname[MAX_PLAYER_NAME], string[120], count; // new variables
    for(new i = 0; i < MAX_PLAYERS; i++) // loops all the players
    {
        if(IsPlayerConnected(i)) // checks if the player is connected
        {
            if(Mute[i] >= 1) // checks if the looped player is muted at the moment
            {
                GetPlayerName(i, iname, sizeof(iname)); // if they are, this retrieves the name
                format(string, sizeof(string), "%s (%d) - %d seconds remaining", iname, i, Mute[i]); // sets the string
                SendClientMessage(playerid, 0xFFFFFFFF, string); // sends the message
                count++; // puts the count to a ++ value, you'll see why further down
            }
        }
    }
    if(count == 0) return SendClientMessage(playerid, 0xFFFFFFFF, "There are no muted players online."); // if there are no muted players online, it'll return this message
    if(count == 1) return SendClientMessage(playerid, 0xFFFFFFFF, "There is 1 player who is muted online."); // if there's just 1 muted player online, it'll returns this message
    if(count >= 2) // if there are more than 1 muted players online, the following will occur
    {
        format(string, sizeof(string), "There are %d amount of players who are online and muted.", count); // sets the string
        SendClientMessage(playerid, 0xFFFFFFFF, string); // sends the message
    }
    return 1;
}
That was another optional command that you might want to add to your server too.

Tutorial: Command /unmuteall:

Sometimes, it's a pain unmuting everyone if you ever wanted to, so let's use this method instead of making a command as follows:

pawn Code:
CMD:unmuteall(playerid, params[])
{
    if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xFFFFFFFF, "{FFFF00}Only admins can use this command."); // checks if the sender is an RCON logged in admin
    new count, string[120]; // new variables
    for(new i = 0; i < MAX_PLAYERS; i++) // loops all the players one-by-one
    {
        if(IsPlayerConnected(i)) // checks if the player is connected
        {
            if(Mute[i] >= 1) // if the looped player has a mute time more than 0
            {
                count++; // the count will go up
                Mute[i] = 0; // and the mute time will be set to 0
            }
        }
    }
    new uname[MAX_PLAYER_NAME]; // this is the name of the sender
    GetPlayerName(playerid, uname, sizeof(uname)); // this will get the playerid (sender)'s name
    format(string, sizeof(string), "%d players have been admin-unmuted by %s (%d)", count, uname, playerid); // sets the string to say how many players were unmuted and by who
    SendClientMessageToAll(0xFFFFFFFF, string); // sends this global message
    return 1;
}
That's about it, you might want to just add the following too.

Tutorial: Command /mutecmds:

This will be a simple command showing the commands available to the player:

pawn Code:
CMD:mutecmds(playerid, params[])
{
    SendClientMessage(playerid, 0xFFFFFFFF, "/mute - Mute a player");
    SendClientMessage(playerid, 0xFFFFFFFF, "/unmute - Unmute a player");
    SendClientMessage(playerid, 0xFFFFFFFF, "/mymute - View your own mute time");
    SendClientMessage(playerid, 0xFFFFFFFF, "/mutelist - View a global list of all the online players who are muted");
    SendClientMessage(playerid, 0xFFFFFFFF, "/unmuteall - Unmute all the players on the server at once");
    return 1;
}
This command will basically send messages to the player stating the commands that are available regarding all the mute stuff, of course, you could just integrate this into your own /help command or whatever.
Reply
#2

Good job thank you
Reply
#3

Quote:
Originally Posted by DanishHaq
View Post
pawn Code:
CMD:mutelist(playerid, params[])
{
    new iname[MAX_PLAYER_NAME], string[120], count; // new variables
    for(new i = 0; i < MAX_PLAYERS; i++) // loops all the players
    {
        if(IsPlayerConnected(i)) // checks if the player is connected
        {
            if(Mute[i] >= 1) // checks if the looped player is muted at the moment
            {
                GetPlayerName(i, iname, sizeof(iname)); // if they are, this retrieves the name
                format(string, sizeof(string), "%s (%d) - %d seconds remaining", iname, i, Mute[i]); // sets the string
                SendClientMessage(playerid, 0xFFFFFFFF, string); // sends the message
                count++; // puts the count to a ++ value, you'll see why further down
            }
        }
    }
    if(count == 0) return SendClientMessage(playerid, 0xFFFFFFFF, "There are no muted players online."); // if there are no muted players online, it'll return this message
    if(count == 1) return SendClientMessage(playerid, 0xFFFFFFFF, "There is 1 player who is muted online."); // if there's just 1 muted player online, it'll returns this message
    if(count >= 2) // if there are more than 1 muted players online, the following will occur
    {
        format(string, sizeof(string), "There are %d amount of players who are online and muted.", count); // sets the string
        SendClientMessage(playerid, 0xFFFFFFFF, string); // sends the message
    }
    return 1;
}
You can use switch for this...

pawn Code:
switch(count)
{
    case 0:
    {
        SendClientMessage(playerid, 0xFFFFFFFF, "There are no muted players online."); // if there are no muted players online, it'll return this message
        return 1;
    }
    case 1:
    {
        SendClientMessage(playerid, 0xFFFFFFFF, "There is 1 player who is muted online."); // if there's just 1 muted player online, it'll returns this message
        return 1;
    }
    default:
    {
        format(string, sizeof(string), "There are %d amount of players who are online and muted.", count); // sets the string
        SendClientMessage(playerid, 0xFFFFFFFF, string); // sends the message
        return 1;
    }
}
Good job, +rep.

edit: You have given out too much Reputation in the last 24 hours, try again later.

errm...
Reply
#4

Good job man!
Reply
#5

Thanks for the feedback .
Reply
#6

Great tutorial, will help many beginners.
Reply
#7

Nice tutorial, it's good explained.
Reply
#8

Thanks, I'll be doing more tutorials soon too <3.
Reply
#9

Hello Danish,
so for me I'll just suggest you something like if he disconect to the servers when he was muted, when he will come back he will be automatically muted back. (it's possible).

Anyways nicely explained.
Reply
#10

Quote:
Originally Posted by Matnix
View Post
Hello Danish,
so for me I'll just suggest you something like if he disconect to the servers when he was muted, when he will come back he will be automatically muted back. (it's possible).

Anyways nicely explained.
Yea I know it's possible by creating files for the mute system, but I couldn't really be bothered then . It's easy to do that anyway, people using this can just merge this with their savable variables too.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)