Command cash
#1

Hello, I'm creating a command, reservedd to admins, witch gives money to a player, from his name.

It's that :

pawn Код:
dcmd_cash(playerid, params[])
{
    new id, string[256], pname[24], amount;
    if (sscanf(params, "s", pname))
    return SendClientMessage(playerid, 0xFF0000AA, "Usage: \"/cash <playername> <amount>\"");
    if (!IsPlayerAdmin(playerid))
    return SendClientMessage(playerid, 0xFF0000AA, "Not admin !");
    else
    {
        id = GetPlayerID(pname);
        if(!IsPlayerConnected(id))
        return SendClientMessage(playerid, 0xFF0000AA, "Player not found");
        else
        {
            GivePlayerMoney(id, amount);
            format(string, sizeof(string), "You gave %s $ to %i.", amount, GetName(id));
            SendClientMessage(playerid, 0xFFFF00AA, string);
            SendClientMessage(id, 0xFFF00AA, "Admin %s gave you %i", playerid, amount);
        }
    }
    return 1;
}
It works when I type /cash <playername>, I have a message that say that I gave money to a player (without amount !), but when I enter amount, it say player not found.

How can I fix that please ? Thanks =)
Reply
#2

Change this:
pawn Код:
new id, string[256], pname[24], amount;
if (sscanf(params, "s", pname))
To:
pawn Код:
new id, string[256], pname[24], amount;
if (sscanf(params, "s[24]i", pname, amount))
From wiki sscanf: sscanf - wiki

Код:
c - A character.
d, i - An integer.
h, x - A hex number (e.g. a colour).
f - A float.
s - A string.
z - An optional string.
pX - An additional delimiter where X is another character.
'' - Encloses a litteral string to locate.
u - User, takes a name, part of a name or an id and returns the id if they're connected.
Reply
#3

I'll try that
Reply
#4

pawn Код:
SendClientMessage(id, 0xFFF00AA, "Admin %s gave you %i", playerid, amount);
^ That will not work because of %s = playerid plus the normal SendClientMessage does not have support to format string(s).

pawn Код:
format( string, sizeof string, "Admin %s gave you %i.", GetName( playerid ), amount );
SendClientMessage( id, 0xFFFF00AA, string );
The code above will work I think.

pawn Код:
format(string, sizeof(string), "You gave %s $ to %i.", amount, GetName(id));
string = amount
integer = playername
?

pawn Код:
format( string, sizeof string, "You gave $%i to %s.", amount, GetName(id));
The code above will work I think.

Also? Why do you use 256 on string? :S
String in sscanf for player name ? xD

Rewritten /givecash
pawn Код:
dcmd_givecash( playerid, params[ ] )
{
    if ( !IsPlayerAdmin( playerid ) ) return SendClientMessage( playerid, 0xAA0000AA, "Error: Not an admin." );
    new targetid, amount;
    if ( sscanf( params, "ud", targetid, amount ) ) return SendClientMessage( playerid, 0xAA0000AA, "Error: /givecash < playerid / partofname > < amount >" );
    if ( targetid == INVALID_PLAYER_ID ) return SendClientMessage( playerid, 0xAA0000AA, "Error: Invalid player." );
    if ( amount < 0 ) return SendClientMessage( playerid, 0xAA0000AA, "Error: Invalid amount." );
    new string[ 128 ];
    format( string, sizeof string, "Admin %s gave you $%d.", GetName( playerid ), amount );
    SendClientMessage( playerid, 0xAA0000AA, string );
    format( string, sizeof string, "You gave $%d to %s.", amount, GetName( targetid ) );
    SendClientMessage( targetid, 0xAA0000AA, string );
    GivePlayerMoney( targetid, amount );
    return 1;
Reply
#5

In fact, I don't know very well hoc to use sscanf x)

I'll try all that thx for tour help


And, how can I use this cmd from a part of name please ?
Reply
#6

Quote:
Originally Posted by iPLEOMAX
Посмотреть сообщение
Change this:
pawn Код:
new id, string[256], pname[24], amount;
if (sscanf(params, "s", pname))
To:
pawn Код:
new id, string[256], pname[24], amount;
if (sscanf(params, "s[24]i", pname, amount))
From wiki sscanf: sscanf - wiki

Код:
c - A character.
d, i - An integer.
h, x - A hex number (e.g. a colour).
f - A float.
s - A string.
z - An optional string.
pX - An additional delimiter where X is another character.
'' - Encloses a litteral string to locate.
u - User, takes a name, part of a name or an id and returns the id if they're connected.
I tryed that, the server returns me "player not found".
Reply
#7

Quote:
Originally Posted by Basicz
Посмотреть сообщение
pawn Код:
SendClientMessage(id, 0xFFF00AA, "Admin %s gave you %i", playerid, amount);
^ That will not work because of %s = playerid plus the normal SendClientMessage does not have support to format string(s).

pawn Код:
format( string, sizeof string, "Admin %s gave you %i.", GetName( playerid ), amount );
SendClientMessage( id, 0xFFFF00AA, string );
The code above will work I think.

pawn Код:
format(string, sizeof(string), "You gave %s $ to %i.", amount, GetName(id));
string = amount
integer = playername
?

pawn Код:
format( string, sizeof string, "You gave $%i to %s.", amount, GetName(id));
The code above will work I think.

Also? Why do you use 256 on string? :S
String in sscanf for player name ? xD

Rewritten /givecash
pawn Код:
dcmd_givecash( playerid, params[ ] )
{
    if ( !IsPlayerAdmin( playerid ) ) return SendClientMessage( playerid, 0xAA0000AA, "Error: Not an admin." );
    new targetid, amount;
    if ( sscanf( params, "ud", targetid, amount ) ) return SendClientMessage( playerid, 0xAA0000AA, "Error: /givecash < playerid / partofname > < amount >" );
    if ( targetid == INVALID_PLAYER_ID ) return SendClientMessage( playerid, 0xAA0000AA, "Error: Invalid player." );
    if ( amount < 0 ) return SendClientMessage( playerid, 0xAA0000AA, "Error: Invalid amount." );
    new string[ 128 ];
    format( string, sizeof string, "Admin %s gave you $%d.", GetName( playerid ), amount );
    SendClientMessage( playerid, 0xAA0000AA, string );
    format( string, sizeof string, "You gave $%d to %s.", amount, GetName( targetid ) );
    SendClientMessage( targetid, 0xAA0000AA, string );
    GivePlayerMoney( targetid, amount );
    return 1;
The command that you game me doesn't work, it returns "Error : /givecash < playerid ....."

Help ?
Reply
#8

Try this, i made it in few minutes. Not sure if it works:
pawn Код:
stock GetIdFromName(playername[]) //Use this stock to use names instead of ids.
{
    for(new i=0; i<MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i) && !IsPlayerNPC(i))
        {
            new pname[MAX_PLAYER_NAME];
            GetPlayerName(i,pname,MAX_PLAYER_NAME);
            if(strfind(pname,playername,true) != -1 && strlen(playername) != 0)
            {
                return i;
            }
        }
    }
    return -1;
}
So, for Example you are going to give cash to someone:

pawn Код:
dcmd_givecash( playerid, params[ ] )
{
        if(IsPlayerAdmin(playerid))
        {
            new string[26], mny;
            if(!sscanf(params,"s[26]i",string,mny))
            {
                if(IsPlayerConnected(rplayer))
                {
                new giveplayerid;
                giveplayerid = GetIdFromName(string);

                new pname[MAX_PLAYER_NAME], tomsg[60];
                GetPlayerName(playerid, pname, sizeof(pname));

                format(tomsg,sizeof(tomsg),"Admin %s gave you %i $!",pname,mny);

                GivePlayerMoney(giveplayerid , mny);

                SendClientMessage(giveplayerid, COLOR_GREEN, tomsg);

                } else return SendClientMessage(playerid, COLOR_RED, "Player is not connected.");
            } else return SendClientMessage(playerid, COLOR_RED, "Usage: /givecash [ part of name ] [ amount ]");
        }
        else SendClientMessage(playerid, COLOR_RED, "You are not an admin!");
        return true;
}
This should work. but UNTESTED. xD
Reply
#9

Quote:
Originally Posted by iPLEOMAX
Посмотреть сообщение
Try this, i made it in few minutes. Not sure if it works:
pawn Код:
stock GetIdFromName(playername[]) //Use this stock to use names instead of ids.
{
    for(new i=0; i<MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i) && !IsPlayerNPC(i))
        {
            new pname[MAX_PLAYER_NAME];
            GetPlayerName(i,pname,MAX_PLAYER_NAME);
            if(strfind(pname,playername,true) != -1 && strlen(playername) != 0)
            {
                return i;
            }
        }
    }
    return -1;
}
So, for Example you are going to give cash to someone:

pawn Код:
dcmd_givecash( playerid, params[ ] )
{
        if(IsPlayerAdmin(playerid))
        {
            new string[26], mny;
            if(!sscanf(params,"s[26]i",string,mny))
            {
                if(IsPlayerConnected(rplayer))
                {
                new giveplayerid;
                giveplayerid = GetIdFromName(string);

                new pname[MAX_PLAYER_NAME], tomsg[60];
                GetPlayerName(playerid, pname, sizeof(pname));

                format(tomsg,sizeof(tomsg),"Admin %s gave you %i $!",pname,mny);

                GivePlayerMoney(giveplayerid , mny);

                SendClientMessage(giveplayerid, COLOR_GREEN, tomsg);

                } else return SendClientMessage(playerid, COLOR_RED, "Player is not connected.");
            } else return SendClientMessage(playerid, COLOR_RED, "Usage: /givecash [ part of name ] [ amount ]");
        }
        else SendClientMessage(playerid, COLOR_RED, "You are not an admin!");
        return true;
}
This should work. but UNTESTED. xD
I tryed the command that you gave me, and it works perfectly. But, to find myself in all my commands, I little changed it, and the copilation works.

It's that :

pawn Код:
dcmd_cash(playerid, params[])
{
    new id, string[26], ammount;
    if (sscanf(params, "s[26]i", string, ammount))
    return SendClientMessage(playerid, 0xFF9900, "Utilisation: \"/Cash <player name> <ammount>\"");
        if (!IsPlayerAdmin(playerid))
    return SendClientMessage(playerid, 0xFF0000AA, "You are not admin");
   
    id = GetPlayerID(string);
   
    if (!IsPlayerConnected(id))
    {
                new pname[MAX_PLAYER_NAME], tomsg[100], adminmsg[100];
                GetPlayerName(playerid, pname, sizeof(pname));

                format(tomsg, sizeof(tomsg), "Admin %s gave you %i $ !", pname, ammount);
                GivePlayerMoney(id , ammount);
                SendClientMessage(id, 0xFFFF00AA, tomsg);
               
                format(adminmsg, sizeof(adminmsg), "You gave %i $ to %s !", ammount, id);
                SendClientMessage(playerid, 0xFFFF00AA, adminmsg);

    }
    else return SendClientMessage(playerid, 0xFF0000AA, "Player not found");

    return true;
}
But, if I enter a part of name or a complete name, the command returns "player not found".

How to fix that please ?

Thx =)
Reply
#10

You should change this:
pawn Код:
if (!IsPlayerConnected(id))
to this:
pawn Код:
if (IsPlayerConnected(id))
! means "not".
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)