ZCMD and sscanf help!
#2

I went into my code, and selected a readable command for you.
Make sure to read the //green text. I typed it so you can understand what it does.

It uses ZCMD and sscanf.

pawn Код:
CMD:pay(playerid, params[])
{
    new target, amount; // We need to create a variable for the target (The person we're giving the money to.) And the amount of money.
    if(sscanf(params, "ui",target, amount)) return SendClientMessage(playerid,COLOR_WHITE, "USAGE: /pay [playerid] [amount]"); //"u" is for the playerid. And "i" is a integer, in this case - the amount.
    if(!IsPlayerConnected(target)) return SendClientMessage(playerid,COLOR_RED, "ERROR: Player is not connected."); // ! means not. If the target is not connected, we send a error message.
    new money = GetPlayerMoney(playerid); // We're creating another variable, which will get the player's current money.
    if(money < amount ) return SendClientMessage(playerid,COLOR_RED, "ERROR: You don't have enough money to give to that person."); //if they don't have the amount of money their trying to pay, then we send a error message.
    if(amount > 500000 || amount < 1) return SendClientMessage(playerid,COLOR_RED, "ERROR: The maximum amount of money you can pay at a time is 500,000. And the minimum is 1."); //If they try to pay more than 500k at a time, we send another error message.
    GivePlayerMoney(playerid, -amount); //We withdraw the amount of money from the player, since he's giving it to the target.
    GivePlayerMoney(target, amount); //We give the target the amount of money the player just gave him.
    new pstring[128]; //we create a new string, which is 128 characters long. I name it pstring for player string.
    new tstring[128]; //same as above, except the target's string.
    format(pstring,sizeof(pstring), "You have given %d$ to %s.",amount,GetName(target)); // we need to format both strings, that will be sent.
    format(tstring,sizeof(tstring), "%s has gave you %d$.",GetName(playerid),amount); //Same as above. We format the target's string he will be receiving.
    SendClientMessage(playerid,COLOR_GREEN, pstring); //we send the playerid a message that he just gave X amount of money to the target.
    SendClientMessage(target,COLOR_GREEN, tstring); //we let the target know he just received the money by the player.
    return 1; //Stop the command.
}
If you still don't understand, let me know.
Reply


Messages In This Thread
ZCMD and sscanf help! - by [DX]Aru12345 - 16.03.2013, 05:10
Re: ZCMD and sscanf help! - by rangerxxll - 16.03.2013, 05:18

Forum Jump:


Users browsing this thread: 1 Guest(s)