[Tutorial] How to make a PM command ( zcmd + sscanf2 )
#1

How to make a /PM command
This is my first tutorial I have ever made.
The tutorial is about how to make a /pm command.

Okay, let's start !

Includes
Make sure you have these includes at your top of your script
pawn Код:
#include < a_samp >
// Includes a_samp.inc
#include < zcmd >
// Includes zcmd, made by ZeeX_
#include < sscanf2 >
// Includes sscanf2, made by ******
Creating the command
We will make this :
" /pm ( id/name ) ( message ) "
pawn Код:
CMD:pm( playerid, params[ ] ) // We will create a new command ( /pm )
{ // Opening bracket
    return true;
} // Closing bracket
Now, let's check if the player didn't enter any parameters. We will need sscanf2 for this.
pawn Код:
CMD:pm( playerid, params[ ] ) // We will create a new command ( /pm )
{ // Opening bracket

    new // creating new variables
        iTarget, // a variable called iTarget
        szMsg[ 100 ]; // a variable called szMsg. This will be a string that can carry 100 characters
       
    if ( sscanf( params, "rs[100]", iTarget, szMsg ) )
    {   // If the player hasn't entered the player or the message ( r = playerID / name || s = string )
        return SendClientMessage( playerid, 0xFF3333AA, "[USAGE]: /pm ( id/name ) ( message )" );
        // Sends the player message about the correct command usage.
    }

        return true;
} // Closing bracket
Read sscanf thread for more information.

Now, let's make sure if the player that the sender has entered is connected.
pawn Код:
CMD:pm( playerid, params[ ] ) // We will create a new command ( /pm )
{ // Opening bracket

    new // creating new variables
        iTarget, // a variable called iTarget
        szMsg[ 100 ]; // a variable called szMsg. This will be a string that can carry 100 characters
       
    if ( sscanf( params, "rs[100]", iTarget, szMsg ) )
    {   // If the player hasn't entered the player or the message ( r = playerID / name || s = string )
        return SendClientMessage( playerid, 0xFF3333AA, "[USAGE]: /pm ( id/name ) ( message )" );
        // Sends the player message about the correct command usage.
    }

        if ( iTarget == INVALID_PLAYER_ID ) // The player is not connected!
    {
        return SendClientMessage( playerid, 0xFF3333AA, "[ERROR]: That player is not connected!" );
        // Sends the player message that he/she has entered a wrong ID.
    }

        return true;
} // Closing bracket
We will check if the target ( iTarget ) is an invalid player, it will send a message to the sender that the player is not connected.

Now let's start making some variables to create the message!
pawn Код:
CMD:pm( playerid, params[ ] ) // We will create a new command ( /pm )
{ // Opening bracket

    new // creating new variables
        iTarget, // a variable called iTarget
        szMsg[ 100 ]; // a variable called szMsg. This will be a string that can carry 100 characters
       
    if ( sscanf( params, "rs[100]", iTarget, szMsg ) )
    {   // If the player hasn't entered the player or the message ( r = playerID / name || s = string )
        return SendClientMessage( playerid, 0xFF3333AA, "[USAGE]: /pm ( id/name ) ( message )" );
        // Sends the player message about the correct command usage.
    }
   
    if ( iTarget == INVALID_PLAYER_ID ) // The player is not connected!
    {
        return SendClientMessage( playerid, 0xFF3333AA, "[ERROR]: That player is not connected!" );
        // Sends the player message that he/she has entered a wrong ID.
    }
   
    new
        szStr[ 128 ];   // Now, we will create another variable called " szStr ".
                        // 128 is the max size for client mesages.
    new
        pName[ MAX_PLAYER_NAME ], // We will create another variable which will store the sender's name
        tName[ MAX_PLAYER_NAME ]; // Same as above, except this is to store the target's name
       
    GetPlayerName( playerid, pName, MAX_PLAYER_NAME );
    // We will get the sender's name and store it at the pName variable
   
    GetPlayerName( iTarget, tName, MAX_PLAYER_NAME );
    // We will get the target's name and store it at the tName variable

        return true;
} // Closing bracket
We will format szStr into a string, pName is for the sender's name and tName is for the target's name.

Now, let's start formatting the messages!
pawn Код:
CMD:pm( playerid, params[ ] ) // We will create a new command ( /pm )
{ // Opening bracket

    new // creating new variables
        iTarget, // a variable called iTarget
        szMsg[ 100 ]; // a variable called szMsg. This will be a string that can carry 100 characters
       
    if ( sscanf( params, "rs[100]", iTarget, szMsg ) )
    {   // If the player hasn't entered the player or the message ( r = playerID / name || s = string )
        return SendClientMessage( playerid, 0xFF3333AA, "[USAGE]: /pm ( id/name ) ( message )" );
        // Sends the player message about the correct command usage.
    }
   
    if ( iTarget == INVALID_PLAYER_ID ) // The player is not connected!
    {
        return SendClientMessage( playerid, 0xFF3333AA, "[ERROR]: That player is not connected!" );
        // Sends the player message that he/she has entered a wrong ID.
    }
   
    new
        szStr[ 128 ];   // Now, we will create another variable called " szStr ".
                        // 128 is the max size for client mesages.
    new
        pName[ MAX_PLAYER_NAME ], // We will create another variable which will store the sender's name
        tName[ MAX_PLAYER_NAME ]; // Same as above, except this is to store the target's name
       
    GetPlayerName( playerid, pName, MAX_PLAYER_NAME );
    // We will get the sender's name and store it at the pName variable
   
    GetPlayerName( iTarget, tName, MAX_PLAYER_NAME );
    // We will get the target's name and store it at the tName variable
   
    // We will format the message.
    format( szStr, sizeof ( szStr ), "[PM from %s]: %s", pName, szMsg );
    // %s is used to format strings, the first %s is the player's name ( pName ),
    // and the second %s is the message. ( szMsg )
   
    SendClientMessage( iTarget, 0x33FF33AA, szStr );
    // Sends the target the PM which the sender has sent.
   
    // We will format another message.
    format( szStr, sizeof ( szStr ), "[PM to %s]: %s", tName, szMsg );
    // %s is used to format strings, the first %s is the target's name ( tName ),
    // and the second %s is the message. ( szMsg )
   
    SendClientMessage( playerid, 0x33FF33AA, szStr );
    // Sends the sender message about the message he has sent.
   
    return true;
} // Closing bracket
Final script
pawn Код:
#include < a_samp >
// Includes a_samp.inc
#include < zcmd >
// Includes zcmd, made by ZeeX_
#include < sscanf2 >
// Includes sscanf2, made by ******

CMD:pm( playerid, params[ ] ) // We will create a new command ( /pm )
{ // Opening bracket

    new // creating new variables
        iTarget, // a variable called iTarget
        szMsg[ 100 ]; // a variable called szMsg. This will be a string that can carry 100 characters
       
    if ( sscanf( params, "rs[100]", iTarget, szMsg ) )
    {   // If the player hasn't entered the player or the message ( r = playerID / name || s = string )
        return SendClientMessage( playerid, 0xFF3333AA, "[USAGE]: /pm ( id/name ) ( message )" );
        // Sends the player message about the correct command usage.
    }
   
    if ( iTarget == INVALID_PLAYER_ID ) // The player is not connected!
    {
        return SendClientMessage( playerid, 0xFF3333AA, "[ERROR]: That player is not connected!" );
        // Sends the player message that he/she has entered a wrong ID.
    }
   
    new
        szStr[ 128 ];   // Now, we will create another variable called " szStr ".
                        // 128 is the max size for client mesages.
    new
        pName[ MAX_PLAYER_NAME ], // We will create another variable which will store the sender's name
        tName[ MAX_PLAYER_NAME ]; // Same as above, except this is to store the target's name
       
    GetPlayerName( playerid, pName, MAX_PLAYER_NAME );
    // We will get the sender's name and store it at the pName variable
   
    GetPlayerName( iTarget, tName, MAX_PLAYER_NAME );
    // We will get the target's name and store it at the tName variable
   
    // We will format the message.
    format( szStr, sizeof ( szStr ), "[PM from %s]: %s", pName, szMsg );
    // %s is used to format strings, the first %s is the player's name ( pName ),
    // and the second %s is the message. ( szMsg )
   
    SendClientMessage( iTarget, 0x33FF33AA, szStr );
    // Sends the target the PM which the sender has sent.
   
    // We will format another message.
    format( szStr, sizeof ( szStr ), "[PM to %s]: %s", tName, szMsg );
    // %s is used to format strings, the first %s is the target's name ( tName ),
    // and the second %s is the message. ( szMsg )
   
    SendClientMessage( playerid, 0x33FF33AA, szStr );
    // Sends the sender message about the message he has sent.
   
    return true;
} // Closing bracket
Have fun!
p.s: This is my first tutorial
Reply
#2

Well explaind good job Keep up the good work
Reply
#3

As he (^^) said but it'd be better if you explained how to do "player to player" commands for people who don't know how to do it. i.e /pay, /pm, /kick etcetra etcetra.. Anyway, good work!
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)