[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


Messages In This Thread

Forum Jump:


Users browsing this thread: 2 Guest(s)