[Tutorial] Simple Private Message System Using zCMD And Sscanf with Logs
#1

READ THIS
In this tutorial we will be creating a Simple Private Message System using zCMD and sscanf.
The Messages will be written in a log so if the owner or any admins wants to see it.

Your proberlly thinking: Ahhh this is so simple. im only making this tutorial because i wonna help noobs out there.

Includes
Add this on top of your script.
pawn Code:
#include <zcmd>
#include <sscanf2>
This will just include the files so we can use anyfhing there is in there.
Include Downloads at bottom!

New's:
Add this under your #include
pawn Code:
new PMEnabled=1;
We will use this later in the command to check if PM Is Enabled for players or not.
Admins Will be able to Enable or Disable

Function for Logs
Add this at top of your script under new's

pawn Code:
forward PMLog(string[]);
this will create our Log Function

public Function
Add this somewhere you want:
pawn Code:
public PMLog(string[])
{
    new pm[128]; // Creates a new string
    format(pm, sizeof(pm), "%s\n", string); // Formats the string;
    new File:hFile; // Creates a new variable with type File
    hFile = fopen("/LOGS/pm.log", io_append); // Opens the Log File
    fwrite(hFile, pm); // Writes the log
    fclose(hFile); // Closes file
}
Commands:
We will be using zCMD and sscanf for Commands:
Add this anywhere you want:
pawn Code:
CMD:pm(playerid, params [ ] ) // Adding the command
{
    if(IsPlayerConnected(playerid)) // Checks if the Player IS connected
    {
        new pID, Message[60],playername[MAX_PLAYER_NAME],targetName[MAX_PLAYER_NAME],string[128],string2[128]; // Adding new's the pID Will hold the Player's ID Message will hold the message and playername will hold the name of the player that sends it and 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 == 0) return SendClientMessage(playerid, 0xAFAFAFAA, "PM Is Disabled");
        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
        PMLog(string2); // Writes to the lgo with our function
        PMLog(string); // Writes to the log
    }
    return 1; // Returns 1
}

And This After:
pawn Code:
CMD:nopm(playerid, params [ ] )
{
    if(IsPlayerConnected(playerid)) // If is player connected
    {
        if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xAFAFAFAA, "Your not a admin"); // Checks if the player isent an Admin RCON
        if(PMEnabled == 1) // If PM Is enabled it will disable it
        {
            SendClientMessageToAll(0xFFFF00AA, "An Admin has disabled Private Messages!"); // Sends a Client Message to all players
            PMEnabled = 0; // Sets the PM Enabled to 0
        }
        else if(PMEnabled == 0) // If PM Is Disabled it will enable it
        {
            SendClientMessageToAll(0xFFFF00AA, "An Admin has enabled Private Messages!"); // Sends a Client MEssage to all playesr
            PMEnabled = 1; // Sets the PM Enabled to 1
        }
    }
    return 1;
}
NOTE: REMEMBER TO CREATE FOLDER IN SCRIPTFILES/Logs

Includes Link: https://sampforum.blast.hk/showthread.php?tid=120356

https://sampforum.blast.hk/showthread.php?tid=91354

Reply
#2

Good tut
Reply
#3

Quote:
Originally Posted by Jason_Thunde
View Post
[SIZE="3"]READ THIS
The Messages will be written in a log so if the owner or any admins wants to see it.
The messages will be printed in a log so the owner/admins can see it if they want.

Your proberlly thinking: Ahhh this is so simple. im only making this tutorial because i wonna help noobs out there.
You're probably thinking: Ahhh this is so simple. I'm only making this tutorial because I want to help the noobs out there.

This will just include the files so we can use anyfhing there is in there.
This will just include the files so we can use anything there is in there.
Better: This will make the include files available for usage in our script. (Search the forum to find them)
I corrected some of your mistakes, my english isn't perfect either but I did my best!

Nice tutorial, you explain everything. It is not another copy-paste tutorial, well done!
Reply
#4

You need to add a length check to your /pm command. Otherwise anyone can join the server and crash it.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)