[Tutorial] Making a Private message system
#1

Private Message System
Introduction

Hello, today i'm going to be showing you how to make a total of two commands, them being a /pm <id> <message>
and /r <message> the r meaning reply.

Things you will be needing

[Inc] Zcmd By zeex. - https://sampforum.blast.hk/showthread.php?tid=91354
[Plugin] Sscanf By ******. - https://sampforum.blast.hk/showthread.php?tid=120356
Basic Pawn Knowledge.

Lets get started

Add this to the top of your script if you haven't done so already:
pawn Code:
#include <a_samp> //Including sa-mp functions
#include <zcmd> //Zcmd, for our commands
#include <sscanf2> //This is for our extensions or params
Now we will need to add a variable, this is to store the id of the last player that pm'ed you.
If you don't know what a variable is, it's like algebra where you have X and x can be equal to something.
pawn Code:
new LastPm[MAX_PLAYERS]; // The MAX_PLAYERS evaluates to 500 being the default maximum players. we need it
// so we can make it relative to a certin player
Now we are going to need a function to get a players name and return it to the script.
pawn Code:
stock PlayerName(playerid)
{
    new name[MAX_PLAYER_NAME]; // Declearing a new string variable named 'name' with a length of MAX_PLAYER_NAME which i think is 24
    GetPlayerName(playerid, name, sizeof(name));// Getting playerid's name and assigning it the the string variable
    return name;// return the name to the script
}
pawn Code:
public OnPlayerConnect(playerid)
{
    LastPm[playerid] = -1;
    return 1;
}
// Setting the variables both to -1 meaning that he hasnt been pmed
public OnPlayerDisconnect(playerid, reason)
{  
    LastPm[playerid] = -1;
    return 1;
}
Now we can get on to the commands!
pawn Code:
CMD:pm(playerid, params[])
{
    new id, string[2][128], message[128]; // a int variable named id, this is to store the id of the player you are pming
    if(sscanf(params,"us[128]",id,message)) return SendClientMessage(playerid, -1, "Syntax: /pm <id> <message>");// this is for your extension aka params, u is for the id s is for the string 'message'
    if(id == playerid) return SendClientMessage(playerid, -1, "You cannot send a pm to yourself");// If the id you want to pm is your id then send a message back that we cant pm ourself!
    if(id == INVALID_PLAYER_ID) return SendClientMessage(playerid, -1, "This player is not connected");// If the id of the player you want to pm is not connect send a message
    format(string[0], 128, "** Pm Sent to %s : %s", PlayerName(id), message);// formatting a string the [0] is part of the array %s is a string and we are using our function PlayerName to return the players name the message variable is equal to what you put
    format(string[1], 128, "** Pm From %s : %s", PlayerName(playerid), message);// same as the above
    SendClientMessage(playerid, -1, string[0]);// Sending the first string
    SendClientMessage(id, -1, string[1]);// sending the string
    LastPm[id] = playerid;// Setting the player you pmed LastPm variable to your id so he can use /r to pm you back
    return 1;
}
pawn Code:
CMD:r(playerid, params[])
{
    new string[2][128], message[128]; // same as the pm command without the id because we are using that in the LastPm variable
    if(LastPm[playerid] == -1) return SendClientMessage(playerid, -1, "No one has pm'ed you since you joined the server"); // when the player connects we set the variable to -1 so it means he hasnt been pm'ed this is just checking if this is true then sending a message
    if(LastPm[playerid] == INVALID_PLAYER_ID) return SendClientMessage(playerid, -1, "The player that last pm'ed you has left the server");// If the above is false and you have been pm'ed before BUT that player has left we send this message
    if(sscanf(params,"s[128]",message)) return SendClientMessage(playerid, -1, "Syntax: /r <message>");// same as the pm command
    format(string[0], 128, "** Pm Sent to %s : %s", PlayerName(LastPm[playerid]), message);// same as the pm command
    format(string[1], 128, "** Pm From %s : %s", PlayerName(playerid), message);//same as the pm command
    SendClientMessage(playerid, -1, string[0]);//same as the pm command
    SendClientMessage(LastPm[playerid], -1, string[1]);//same as the pm command
    LastPm[LastPm[playerid]] = playerid;// If the guy we just replied to got a message from someone else before we replied we can then set it back to our id since we just pmed him
    return 1;
}
I hope this helped you, i'm willing to explain more if needed.
Reply
#2

Nice man, great explanation
Reply
#3

Thanks
Reply
#4

Good job
Reply
#5

Are you sure? I've tested the code and I get no errors in the console.
Reply
#6

Ok, I'm at school atm ill look into the errorrs when I get home
Reply
#7

this is nice
Reply
#8

Simple but nice, it would be better if you had used stuff like Read Able Pms for Admins..
Reply
#9

ehm you forgot something
pawn Code:
if(sscanf(params,"us[128]",id,message)) return SendClientMessage(playerid, -1, "Syntax: /pm <id> <message>");// this is for your extension aka params, u is for the id s is for the string 'message'
and also here
pawn Code:
if(sscanf(params,"s[128]",message)) return SendClientMessage(playerid, -1, "Syntax: /r <message>");// same as the pm command
You have to add the string length else you will get warning at your console
Reply
#10

Ah ok that's what I forgot, editing it now.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)