25.11.2012, 20:05
(
Last edited by thefatshizms; 26/11/2012 at 11:54 AM.
)
Private Message System
IntroductionHello, 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
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
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;
}
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;
}