[Tutorial] Simple Personal Messages with blocking feature
#1

You'll also see this tutorial on Guitar Helping.
Today I'm going to teach you a simple way to create your very own Personal Messaging system with a very useful feature, which is .. Blocking the messages! I hope that my humble tutorial help you with your learning stages in PAWN.

So, we need the following includes before we start Alright! After you get them, go to your script or filterscript and write the following under #include <a_samp>
pawn Code:
#include <zcmd>
#include <sscanf2>
*If you have them, then that's good and move to the next step.

After adding the includes we want to add the definition of our Personal Messages block feature. So, write the following under #include <sscanf2>.
pawn Code:
new PMEnabled[MAX_PLAYER_NAME]; // Defnining PMEnabled with the MAX_PLAYER_NAME
Good! Now let's move to the next step which is spreading our PMEnabled to your script. Put PMEnabled under OnPlayerConnect, like the following example:
pawn Code:
public OnPlayerConnect(playerid)
{
    PMEnabled[playerid] = 1; //Putting this will automatically toggles the connected player's PMs ON when he/she connects.
    return 1;
}
Very good! Now, let's create the command for our simple Personal Messages.
pawn Code:
CMD:pm(playerid, params [ ] ) // The command
{
    if(IsPlayerConnected(playerid)) //This one checks if the player is connected to the server or not.
    {
        new pID, Message[60],playername[MAX_PLAYER_NAME],targetName[MAX_PLAYER_NAME],string[128],string2[128]; // defining the pID which will hold the Player's ID. Message will hold the message and playername will hold the name of the player that sends it, also target to the target
        if(sscanf(params, "us[60]", pID, Message)) return SendClientMessage(playerid, 0xAFAFAFAA, "USAGE: /pm [PlayerID/PlayerName] [Message]"); // Checks if the player has filed in both params if not it will return a error message with Color Grey
        if(pID == INVALID_PLAYER_ID) return SendClientMessage(playerid, 0xAFAFAFAA, "Invalid Player"); // Checks if the player he attemps to send to is invalid if not it will send and error message
        if(PMEnabled[playerid] == 0) return SendClientMessage(playerid, 0xAFAFAFAA, "PM Is Disabled"); // This checks if the PMs are toggled off
        GetPlayerName(pID, targetName, sizeof(targetName)); // Gets the Name of The Target 1st param and stores it in targetName
        GetPlayerName(playerid, playername, sizeof(playername));
        format(string, sizeof(string), "Private Message from %s: %s", playername, Message); // Will format the string the first %s is the name of the guy who sent pm second %s in the Message
        format(string2, sizeof(string2), "Private Message sent to %s: %s", targetName, Message); // The same as above but this time it shows the name off the player you sent it to
        SendClientMessage(playerid, 0xFFFF00AA, string2); // Sends a message to the player who sent the PM
        SendClientMessage(pID, 0xFFFF00AA, string); // Sends a Message to Target
    }
    return 1; // Returns 1
}
Studied the command? All good? Excellent, now write it and try to make it unique more than this into your script!

.. For our next part, we need to focus because it's the command that blocks and unblocks the messages. So, study the following and write it into your script!
pawn Code:
CMD:togpm( playerid, params[ ] )
{
    if(isnull(params)) return SendClientMessage(playerid, 0xFFFFFFFF, "CMD: /togpm [on/off]"); //If the player only types /togpm this message shows.
    if(strcmp(params, "on", true ) == 0 )//This says if the player wrote "on"
    { //If he wrote "on" Do this:
        PMEnabled[playerid] = 1; //Enables or unblocks Personal Messages to arrive.
        SendClientMessage(playerid, 0x00FF00FF, "You have unblocked your Personal Messaging arrival."); //The messages that sends when the player toggled on/unblocked his PMs
    }
    else if( strcmp( params, "off", true ) == 0 ) // This one says, Else if the player types "off"
    { // If he wrote "off" Do this:
        PMEnabled[playerid] = 0; //Disables or blocks Personal Messages from arriving.
        SendClientMessage(playerid, 0xAA3333AA, "You have blocked Personal Messages from arriving."); //The message that sends when the player toggled off/blocked his PMs
    }
    else SendClientMessage(playerid, -1,"CMD: /togpm [on/off]"); //This one says like the first line, if the player only types /togpm, this message shows.
    return 1;
}
Now after studying and writing the last stuff into your script/filterscript hit "F5" or "Compile" button. I hope you learned something today.. And for those who didn't get it working, here is a filterscript that I made for you,

pawn Code:
/*


This tutorial was originally written by Guitar.
http://www.guitarhelping.weebly.com
http://www.sa-mp.com


Thank you for looking at my tutorials.
*/


#include <a_samp>
#include <zcmd>
#include <sscanf2>

new PMEnabled[MAX_PLAYER_NAME]; // Defnining PMEnabled with the MAX_PLAYER_NAME

public OnFilterScriptInit()
{
    print("\n----------------------------------");
    print(" Personal Messaging system has been loaded.");
    print("----------------------------------\n");
    return 1;
}

public OnFilterScriptExit()
{
    print("\n----------------------------------");
    print(" Personal Messaging system has been unloaded.");
    print("----------------------------------\n");
    return 1;
}

main()
{
    print("\n----------------------------------");
    print(" Guitar's tutorial on making a simple Personal Messages with blocking feature.");
    print("----------------------------------\n");
}

CMD:pm(playerid, params [ ] ) // The command
{
    if(IsPlayerConnected(playerid)) //This one checks if the player is connected to the server or not.
    {
        new pID, Message[60],playername[MAX_PLAYER_NAME],targetName[MAX_PLAYER_NAME],string[128],string2[128]; // defining the pID which will hold the Player's ID. Message will hold the message and playername will hold the name of the player that sends it, also target to the target
        if(sscanf(params, "us[60]", pID, Message)) return SendClientMessage(playerid, 0xAFAFAFAA, "USAGE: /pm [PlayerID/PlayerName] [Message]"); // Checks if the player has filed in both params if not it will return a error message with Color Grey
        if(pID == INVALID_PLAYER_ID) return SendClientMessage(playerid, 0xAFAFAFAA, "Invalid Player"); // Checks if the player he attemps to send to is invalid if not it will send and error message
        if(PMEnabled[playerid] == 0) return SendClientMessage(playerid, 0xAFAFAFAA, "PM Is Disabled"); // This checks if the PMs are toggled off
        GetPlayerName(pID, targetName, sizeof(targetName)); // Gets the Name of The Target 1st param and stores it in targetName
        GetPlayerName(playerid, playername, sizeof(playername));
        format(string, sizeof(string), "Private Message from %s: %s", playername, Message); // Will format the string the first %s is the name of the guy who sent pm second %s in the Message
        format(string2, sizeof(string2), "Private Message sent to %s: %s", targetName, Message); // The same as above but this time it shows the name off the player you sent it to
        SendClientMessage(playerid, 0xFFFF00AA, string2); // Sends a message to the player who sent the PM
        SendClientMessage(pID, 0xFFFF00AA, string); // Sends a Message to Target
    }
    return 1; // Returns 1
}

CMD:togpm( playerid, params[ ] )
{
    if(isnull(params)) return SendClientMessage(playerid, 0xFFFFFFFF, "CMD: /togpm [on/off]"); //If the player only types /togpm this message shows.
    if(strcmp(params, "on", true ) == 0 )//This says if the player wrote "on"
    { //If he wrote "on" Do this:
        PMEnabled[playerid] = 1; //Enables or unblocks Personal Messages to arrive.
        SendClientMessage(playerid, 0x00FF00FF, "You have unblocked your Personal Messaging arrival."); //The messages that sends when the player toggled on/unblocked his PMs
    }
    else if( strcmp( params, "off", true ) == 0 ) // This one says, Else if the player types "off"
    { // If he wrote "off" Do this:
        PMEnabled[playerid] = 0; //Disables or blocks Personal Messages from arriving.
        SendClientMessage(playerid, 0xAA3333AA, "You have blocked Personal Messages from arriving."); //The message that sends when the player toggled off/blocked his PMs
    }
    else SendClientMessage(playerid, -1,"CMD: /togpm [on/off]"); //This one says like the first line, if the player only types /togpm, this message shows.
    return 1;
}

public OnPlayerConnect(playerid)
{
    PMEnabled[playerid] = 1; //Putting this will automatically toggles the connected player's PMs ON when he/she connects.
    return 1;
}
Copy all that into a new.pwn file and compile.
Reply
#2

Nice tutorial, even though I had this command included into my "ZCMD - Command Collection", as you can see in my signature! The only thing which I got less is the toggling / switchting between the enabling / disabling of PMs...
Reply
#3

Quote:
Originally Posted by Twisted_Insane
View Post
Nice tutorial, even though I had this command included into my "ZCMD - Command Collection", as you can see in my signature! The only thing which I got less is the toggling / switchting between the enabling / disabling of PMs...
Oh, well um I'm sorry if they're close to each other, I remember creating it for my Gamemode, but I decided to give it out as a tutorial! But, thank you for the positive reply.
Reply
#4

replace
pawn Code:
new PMEnabled[MAX_PLAYER_NAME]; // Defnining PMEnabled with the MAX_PLAYER_NAME
with
pawn Code:
new PMEnabled[MAX_PLAYERS]; // Defnining PMEnabled with the MAX_PLAYERS
Reply
#5

Quote:
Originally Posted by CookieJar
View Post
replace
pawn Code:
new PMEnabled[MAX_PLAYER_NAME]; // Defnining PMEnabled with the MAX_PLAYER_NAME
with
pawn Code:
new PMEnabled[MAX_PLAYERS]; // Defnining PMEnabled with the MAX_PLAYERS
Thank you, but I'd like to know what's the difference first .
Reply
#6

MAX_PLAYER_NAME is 24, MAX_PLAYERS is 500 (or 800). You use MAX_PLAYER_NAME for specifying the size for variables which involve a player name, for example, when you use GetPlayerName(), you would set the size to MAX_PLAYER_NAME.

For creating arrays which should work for all players, you use MAX_PLAYERS.

At this rate, only ID 0-23 have access to PMEnabled and your array will trigger an out-of-bounds error if someone with an ID higher than 23 (IDs start at 0, not 1) would trigger this error.
Reply
#7

Nice tutorial.
Reply
#8

Also, don't forget to add
pawn Code:
if(pID == playerid) return SendClientMessage(playerid,0xAFAFAFAA,"You can't send a pm to yourself");
Reply
#9

You could just make it so you could type "/pm off" instead of making a separate command (/togpm). Just compare params to "off" and if it's true, turn them off, and compare the params to "on", and if it's true, turn them on. Just some feedback, otherwise, good tutorial.
Reply
#10

Nice tutorial.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)