Admin command to giveweapon
#1

Hello,

I'm a beginner in scripting and I'm trying to build my own admin command to give weapon to a player.

The command would look like this: /agiveweapon [id] [weapon] [ammo]

I have searched many times how to fix my errors but some still appears and I don't know what to do to make the script understand I want to give Y weapon to X player with Z ammo.

Please tell me what's wrong and how should I do it.
> By the way SORRY i'm using strcmp my gamemode is running with it, so I have to deal with it.

pawn Код:
if(strcmp(cmd, "/agiveweapon", true) == 0)
    {
        tmp = strtok(cmdtext, idx);
        new tmp2[256];
        new tmp3[256];
        tmp = strtok(cmdtext, idx);
        tmp2 = strtok(cmdtext, idx);
        tmp3 = strtok(cmdtext, idx);
        new id;
        new gun = strval(tmp2);
        new ammo = strval(tmp3);
        id = strval(tmp);
        gun = strval(tmp2);
        ammo = strval(tmp3);
        GetPlayerName(giveplayerid, giveplayer, sizeof(giveplayer));
        GetPlayerName(playerid, sendername, sizeof(sendername));
        PlayerInfo[playerid][pAdmin] = dini_Int(udb_encode(sendername), "level");
        PlayerInfo[giveplayerid][pAdmin] = dini_Int(udb_encode(giveplayer), "level");
}
        if(logged[playerid] == 0)
        {
            SendClientMessage(playerid, COLOR_BRIGHTRED, "You must be logged in!");
            return 1;
        }
        if(PlayerInfo[playerid][pAdmin] < 1)
        {
            SendClientMessage(playerid, COLOR_BRIGHTRED, "You are not allowed to use that command!");
            return 1;
        }
        if(!strlen(tmp))
        {
            SendClientMessage(playerid, COLOR_LIGHTBLUE, "USAGE: /agiveweapon [id] [arme] [munitions]");
            return 1;
        }
        if(!strlen(tmp2))
        {
            SendClientMessage(playerid, COLOR_LIGHTBLUE, "USAGE: /agiveweapon [id] [arme] [munitions]");
            return 1;
        }
        if (IsPlayerConnected(giveplayerid))
        {
            GivePlayerWeapon(playerid, gun, ammo); // error 035: argument type mismatch (argument 2)
            format(string, sizeof(string), "You have given %s a %d with %d ammo.", giveplayer, gun, ammo);
            SendClientMessage(playerid, COLOR_ORANGE, string);
        }
        else {
            format(string, sizeof(string), "ID:%d is not an active player.", id);
            SendClientMessage(playerid, COLOR_BRIGHTRED, string);
                return 1;
}
The command doesn't work despite I fix the error.

Thank you very much.
Reply
#2

Why don't use ZCMD and sscanf? It's faster, easier and far better than this.
Reply
#3

Yes I know, but the gamemode I use runs with strcmp so I can't use it.
Reply
#4

It's weird, but for some apparent reason "beginner" scripters always tend to start with the most outdated and/or complicated of scripts. I really don't know why that is.

The biggest underlying problem is that you are using a mixture of "id" and "giveplayerid". Where the latter one comes from, I don't know because you don't declare it. Use either of those but not a mixture of both.

Furthermore, you can only get the name of the target player after you have verified that this player is, in fact, connected. You'll also want to give the weapon to the target player instead of the player that executes the command.

Here is the command in a modern command processor, ZCMD. I'm not going to provide the solution in old-fashioned strcmp because a) I find that to be counterproductive and b) the code will likely be copy-pasted and you will still not understand what is going on.

PHP код:
COMMAND:agiveweapon(playeridparams[])
{
    if(!
logged[playerid])
        return 
SendClientMessage(playeridCOLOR_BRIGHTRED"You must be logged in!");
    
    if(
PlayerInfo[playerid][pAdmin] < 1)
        return 
SendClientMessage(playeridCOLOR_BRIGHTRED"You are not allowed to use that command!");
    
    new
        
targetid INVALID_PLAYER_ID,
        
weaponid,
        
ammo;
    
    if(
sscanf(params"uii"targetidweaponid ammo))
        return 
SendClientMessage(playeridCOLOR_LIGHTBLUE"USAGE: /agiveweapon [id] [arme] [munitions]");
        
    if(!
IsPlayerConnected(targetid))
    {            
        
format(stringsizeof(string), "ID:%d is not an active player."targetid);
        
SendClientMessage(playeridCOLOR_BRIGHTREDstring);
        return 
1;
    }

    new
        
targetName[MAX_PLAYER_NAME],
        
weaponName[32];
        
    
GetPlayerName(targetidtargetNamesizeof targetName);
    
GetWeaponName(weaponidweaponNamesizeof weaponName);
    
    
GivePlayerWeapon(targetidweaponidammo);
    
    
format(stringsizeof(string), "You have given %s a %d with %d ammo."targetNameweaponNameammo);
    
SendClientMessage(playeridCOLOR_ORANGEstring);
    return 
1;

Reply
#5

First thank you for helping me.

I understand what you mean, but I done much to the gamemod without zcmd, I can't re-do one.

I tried to "translate" and understand what you gave me into "without zcmd" language.

pawn Код:
if(strcmp(cmd, "/agiveweapon", true) == 0)
    {
            if(logged[playerid] == 0)
        {
            SendClientMessage(playerid, COLOR_BRIGHTRED, "You must be logged in.");
            return 1;
        }
       
            if(PlayerInfo[playerid][pAdmin] < 1)
        {
            SendClientMessage(playerid, COLOR_BRIGHTRED, "You are not allowed to use this command!");
            return 1;
        }
           new targetid = giveplayerid;
           new weaponid;
           new ammo;
        if(!strlen(tmp))
        {
            SendClientMessage(playerid, COLOR_LIGHTBLUE, "USAGE: /agiveweapon [id] [arme] [munitions]");
            return 1;
        }
        if (IsPlayerConnected(targetid))
    {
            new targetName[MAX_PLAYER_NAME];
            new weaponName[32];
            GetPlayerName(targetid, targetName, sizeof targetName);
            GetWeaponName(weaponid, weaponName, sizeof weaponName);
            GivePlayerWeapon(targetid, weaponid, ammo);
            format(string, sizeof(string), "You have given %s a %s with %d ammo.", targetName, weaponName, ammo);
            SendClientMessage(playerid, COLOR_ORANGE, string);
            return 1;
    }
        else
    {
            format(string, sizeof(string), "ID:%d is not an active player.", targetid);
            SendClientMessage(playerid, COLOR_BRIGHTRED, string);
    }
   
    return 1;
}
No errors but doesn't work whatever I'm writing:

Reply
#6

Use sscanf2 and take the Vince's code. You don't have to re-wrote your whole gm, use sscanf for your next commands.
Reply
#7

Ok, so I #include <sscanf2> and I copy/past his code and I got 7 errors!


Vince's code:
pawn Код:
COMMAND:agiveweapon(playerid, params[]) // 4551
{
    if(!logged[playerid])
        return SendClientMessage(playerid, COLOR_BRIGHTRED, "You must be logged in!");

    if(PlayerInfo[playerid][pAdmin] < 1)
        return SendClientMessage(playerid, COLOR_BRIGHTRED, "You are not allowed to use that command!");

    new
        targetid = INVALID_PLAYER_ID, // 4560
        weaponid,
        ammo;

    if(sscanf(params, "uii", targetid, weaponid ammo)) // 4564
        return SendClientMessage(playerid, COLOR_LIGHTBLUE, "USAGE: /agiveweapon [id] [arme] [munitions]");

    if(!IsPlayerConnected(targetid))
    {
        format(string, sizeof(string), "ID:%d is not an active player.", targetid);
        SendClientMessage(playerid, COLOR_BRIGHTRED, string);
        return 1;
    }

    new
        targetName[MAX_PLAYER_NAME],
        weaponName[32];

    GetPlayerName(targetid, targetName, sizeof targetName);
    GetWeaponName(weaponid, weaponName, sizeof weaponName);

    GivePlayerWeapon(targetid, weaponid, ammo);

    format(string, sizeof(string), "You have given %s a %d with %d ammo.", targetName, weaponName, ammo);
    SendClientMessage(playerid, COLOR_ORANGE, string);
    return 1;
}
My errors:
pawn Код:
\pawno\include\sscanf2.inc(27) : error 021: symbol already defined: "sscanf"
\pawno\include\sscanf2.inc(28) : error 021: symbol already defined: "unformat"
(4551) : error 017: undefined symbol "agiveweapon"
(4560) : warning 219: local variable "targetid" shadows a variable at a preceding level
(4564) : error 017: undefined symbol "params"
(4564) : warning 215: expression has no effect
(4564) : error 001: expected token: ";", but found ")"
(4564) : error 029: invalid expression, assumed zero
(4564) : fatal error 107: too many error messages on one line
Reply
#8

Show the part of your includes
Reply
#9

This ?

pawn Код:
//===============================Include Files==================================
#include <a_samp>
#include <dini>
#include <dudb>
#include <time>
#include <file>
#include <dutils>
#include <float>
#include <dcmd>
Or that?

pawn Код:
native sscanf(const data[], const format[], {Float,_}:...);
native unformat(const data[], const format[], {Float,_}:...) = sscanf;
Btw, I only got these two lines in sscanf2.inc maybe I got the wrong one.
Reply
#10

Where is
Quote:

#include <sscanf2>
#include <zcmd>

add em
remove this natives
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)