[Tutorial] setskin
#1

Hey Guys


Today we are making an SetSkin command.

We need 2 includes, zcmd and sscanf. (They are no made by me!)

Are you ready? Ok let's start.
We will do it like this(put it on the top of your script), and the color what we will use.
pawn Код:
#define COLOR_RED       0xFF0000FF
Now also under #include <a_samp> add:
pawn Код:
#include <zcmd>
#include <sscanf2>
Thats why we downloaded ZCMD and SSCANF.
Ok when you finished all this lets start making the command
pawn Код:
CMD:setskin(playerid, params[])
{
     
     return 1;
}
So what we did here we added "return 1;" which stands for that the command is succesfuly exicuted.
Now lets do this:
pawn Код:
CMD:setskin(playerid, params[])
{
     new pID,sID,name[MAX_PLAYER_NAME];
     return 1;
}
Why we added pID? pID=TargetID the target of setskin, sID=SkinID the skin that we will set the target and name= PlayerName= SenderName we are getting the sender name and str= string. You are asking why ? You will know later.
Now we will add more stuff to it:
pawn Код:
CMD:setskin(playerid, params[])
{
    if(!IsPlayerAdmin(playerid))return 0;
    new pID,sID, name[MAX_PLAYER_NAME];
    GetPlayerName(playerid, name, sizeof(name));
    return 1;
}
We added IsPlayerAdmin = If the player is Rcon Admin if not that the command return false(0) and GetPlayerName= Getting the sendername/playername.
Ok you are not cunfused any more ? Greate lets add more stuff:
pawn Код:
CMD:setskin(playerid, params[])
{
    if(IsPlayerAdmin(playerid))return 0;
    new pID,sID, name[MAX_PLAYER_NAME];
    GetPlayerName(playerid, name, sizeof(name));
    if(sscanf(params, "ui",pID,sID))return SendClientMessage(playerid, COLOR_RED,"INFO: /setskin [PlayerID/Name] [SkinID]");
    return 1;
}
We added SSCANF= Params if the player writes setskin only its shows /setskin [PlayerID/Name] [SkinID].
Ok lets finish the command:
pawn Код:
CMD:setskin(playerid, params[])
{
    if(IsPlayerAdmin(playerid))return 0;
    new pID,sID, name[MAX_PLAYER_NAME], str[128];
    GetPlayerName(playerid, name, sizeof(name));
    if(sscanf(params, "ui",pID,sID))return SendClientMessage(playerid, COLOR_RED,"INFO: /setskin [PlayerID/Name] [SkinID]");
    if(pID == INVALID_PLAYER_ID)return SendClientMessage(playerid, COLOR_RED, "Player not found.");
    if(sID > 299)return SendClientMessage(playerid, COLOR_RED, "Skin not found.");
    SetPlayerSkin(pID,sID);
    format(str, sizeof(str), "Admin %s has set your skin to %i.", name, sID);
    SendClientMessage(pID, COLOR_RED,str);
    return 1;
}
We added SetPlayerSkin , and used sscanf and formated the message to use in SCM(SendClientMessage) also checked if the pID is connected and the sID is valid.
Now we are ready to use the command in our server!
Reply
#2

There are several problems with the command above:

- You don't check whether the player (pID) is connected.
- You don't check whether the skin (sID) is a valid one and some invalid skins can crash the client. Valid skins are 0-73 and 75-299.
- You don't define COLOR_RED so it will give undefined symbol.
- SCM is defined as SendClientMessage. That is fine but the line:
pawn Код:
SCM(playerid, COLOR_RED,"Admin %s has set your skin to %i.", name, sID);
is not correct. You need format a string first and then send the formatted text.
Reply
#3

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
There are several problems with the command above:

- You don't check whether the player (pID) is connected.
- You don't check whether the skin (sID) is a valid one and some invalid skins can crash the client. Valid skins are 0-73 and 75-299.
- You don't define COLOR_RED so it will give undefined symbol.
- SCM is defined as SendClientMessage. That is fine but the line:
pawn Код:
SCM(playerid, COLOR_RED,"Admin %s has set your skin to %i.", name, sID);
is not correct. You need format a string first and then send the formatted text.
Huh didnt see that thanks for the answer gonna edit
Reply
#4

Search and Replace. SCM. SendClientMessage. Sorry, I really can't stand that.
Reply
#5

pawn Код:
if(IsPlayerAdmin(playerid))return 0;
Should Be
pawn Код:
if(!IsPlayerAdmin(playerid))return 0;
Looks at my Tut (link in my Signature) it teaches about format.
Reply
#6

pawn Код:
if(sID > 299)return SendClientMessage(playerid, COLOR_RED, "Skin not found.");
Would not it be:

pawn Код:
if(strlen(sID) > 299) return SendClientMessage(playerid, COLOR_RED, "Skin not found.");
Reply
#7

There are problems again.

- As newbie scripter said, only players who are NOT RCON admins can use the command.
- You don't check if the skin is negative or 74 (invalid).
- You send the message to the player who typed the command instead of the player whose skin is set.
- You don't need 128 as size for the string, the message is shorter.
- In case the parameters are wrong, the usage will be displayed. If so, why to declare the name and get the name of the player (who typed the command) when it will be never used?

It can be improved:
pawn Код:
CMD:setskin(playerid, params[])
{
    if (!IsPlayerAdmin(playerid)) return 0;
   
    new
        pID,
        sID;
   
    if (sscanf(params, "ri", pID, sID)) return SendClientMessage(playerid, COLOR_RED, "INFO: /setskin [PlayerID/Name] [SkinID]");
    if (pID == INVALID_PLAYER_ID) return SendClientMessage(playerid, COLOR_RED, "Player not found.");
    if (!(0 <= sID <= 299) || sID == 74) return SendClientMessage(playerid, COLOR_RED, "Skin not found.");
   
    SetPlayerSkin(pID, sID);
   
    new
        name[MAX_PLAYER_NAME],
        str[57];
   
    GetPlayerName(playerid, name, MAX_PLAYER_NAME);
    format(str, sizeof (str), "Admin %s has set your skin to %i.", name, sID);
    SendClientMessage(pID, COLOR_RED, str);
    return 1;
}
Quote:
Originally Posted by EnzoMetlc
Посмотреть сообщение
pawn Код:
if(sID > 299)return SendClientMessage(playerid, COLOR_RED, "Skin not found.");
Would not it be:

pawn Код:
if(strlen(sID) > 299) return SendClientMessage(playerid, COLOR_RED, "Skin not found.");
No, sID is integer not a string to get its lenght.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)