Learning Sscanf
#1

Guys as Title say i am Learning with Sscanf and i modyfied the Maded code with sscanf on wiki samp but i have problem

this is my OnPlayerCommandText

Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
	dcmd(givecash, 8, cmdtext);
	return 0;
}

dcmd_givecash(playerid, params[])
{
  new string[256];
  new playername[MAX_PLAYER_NAME];
	GetPlayerName(playerid, playername, MAX_PLAYER_NAME);
	new
		giveplayerid,
		amount;
	if (sscanf(params, "ud", giveplayerid, amount)) SendClientMessage(playerid, 0xFF9900AA, "Usage: /givecash [playerid/partname] [amount]");
	else if (giveplayerid == INVALID_PLAYER_ID) SendClientMessage(playerid, 0xFF9900AA, "Player Not Found");
	else if (amount > GetPlayerMoney(playerid)) SendClientMessage(playerid, 0xFF9900AA, "You Don't Have Enough Money!");
	else if (giveplayerid == playerid) SendClientMessage(playerid,0xFF9900AA,"You Can't Give Money To Yourself!");
	{
		GivePlayerMoney(giveplayerid, amount);
		GameTextForPlayer(giveplayerid,"~g~Money Recived",3000,5);
		format(string, sizeof(string), "You Recived $%d From Player %s(%d)",amount,playername,giveplayerid);
		SendClientMessage(giveplayerid,0x33CCFFAA,string);
		
		GivePlayerMoney(playerid, 0 - amount);
		GameTextForPlayer(playerid,"~y~Money Sent",3000,5);
		format(string, sizeof(string), "You Have Sent $%d To Player %s(%d)",amount,playername,playerid);
		SendClientMessage(playerid,0x33CCFFAA,string);
	}
	return 1;
}
But When I Type Just /givecash without any id or amount it says those 2 messages ''You Recived $(0) FromPlayer (myname,myid)''
and this one ''You Have Sent $0 To Player (myname,myid)"

but i want if there is player sending to himself it will say ''You Cant Give Money Your Self''...
and if player just type /givecash there will write ''Usage:/givecash playerid/partname amount''

actualy when i wrote givecash in game, i am missing those ''Error Messages''

so can u help ? mybe copy and paste this code to your code and try and see the problem please i was trying to add return1; but then i get errors .... unreachable code etc ...

Ty For any help i will check forum tomorow now i go watch TV

good night all bb
Reply
#2

pawn Код:
dcmd_givecash(playerid, params[])
{
    if (sscanf(params, "ud", giveplayerid, amount)) return SendClientMessage(playerid, 0xFF9900AA, "Usage: /givecash [playerid/partname] [amount]");
   
    new
        string[256],
        playername[MAX_PLAYER_NAME],
        giveplayerid,
        amount;
   
    if (giveplayerid == INVALID_PLAYER_ID) return SendClientMessage(playerid, 0xFF9900AA, "Player Not Found");
    if (amount > GetPlayerMoney(playerid)) return SendClientMessage(playerid, 0xFF9900AA, "You Don't Have Enough Money!");
    if (giveplayerid == playerid) return SendClientMessage(playerid,0xFF9900AA,"You Can't Give Money To Yourself!");

    GetPlayerName(playerid, playername, MAX_PLAYER_NAME);
    GivePlayerMoney(giveplayerid, amount);
    GameTextForPlayer(giveplayerid,"~g~Money Recived",3000,5);
    format(string, sizeof(string), "You Recived $%d From Player %s(%d)",amount,playername,giveplayerid);
    SendClientMessage(giveplayerid,0x33CCFFAA,string);
   
    GivePlayerMoney(playerid, 0 - amount);
    GameTextForPlayer(playerid,"~y~Money Sent",3000,5);
    format(string, sizeof(string), "You Have Sent $%d To Player %s(%d)",amount,playername,playerid);
    SendClientMessage(playerid,0x33CCFFAA,string);

    return 1;
}
Reply
#3

Quote:
Originally Posted by mprofitt
pawn Код:
dcmd_givecash(playerid, params[])
{
    if (sscanf(params, "ud", giveplayerid, amount)) return SendClientMessage(playerid, 0xFF9900AA, "Usage: /givecash [playerid/partname] [amount]");
   
    new
        string[256],
        playername[MAX_PLAYER_NAME],
        giveplayerid,
        amount;
   
    if (giveplayerid == INVALID_PLAYER_ID) SendClientMessage(playerid, 0xFF9900AA, "Player Not Found");
    else if (amount > GetPlayerMoney(playerid)) SendClientMessage(playerid, 0xFF9900AA, "You Don't Have Enough Money!");
    else if (giveplayerid == playerid) SendClientMessage(playerid,0xFF9900AA,"You Can't Give Money To Yourself!");
    else
    {
        GetPlayerName(playerid, playername, MAX_PLAYER_NAME);
        GivePlayerMoney(giveplayerid, amount);
        GameTextForPlayer(giveplayerid,"~g~Money Recived",3000,5);
        format(string, sizeof(string), "You Recived $%d From Player %s(%d)",amount,playername,giveplayerid);
        SendClientMessage(giveplayerid,0x33CCFFAA,string);
       
        GivePlayerMoney(playerid, 0 - amount);
        GameTextForPlayer(playerid,"~y~Money Sent",3000,5);
        format(string, sizeof(string), "You Have Sent $%d To Player %s(%d)",amount,playername,playerid);
        SendClientMessage(playerid,0x33CCFFAA,string);
    }
    return 1;
}
Fail. Use this:
pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
    dcmd(givecash, 8, cmdtext);
    return 0;
}

dcmd_givecash(playerid, params[])
{
  new string[256];
  new playername[MAX_PLAYER_NAME];
    GetPlayerName(playerid, playername, MAX_PLAYER_NAME);
    new
        giveplayerid,
        amount;
    if (sscanf(params, "ud", giveplayerid, amount))return SendClientMessage(playerid, 0xFF9900AA, "Usage: /givecash [playerid/partname] [amount]");
    else if (giveplayerid == INVALID_PLAYER_ID)return SendClientMessage(playerid, 0xFF9900AA, "Player Not Found");
    else if (amount > GetPlayerMoney(playerid))return SendClientMessage(playerid, 0xFF9900AA, "You Don't Have Enough Money!");
    else if (giveplayerid == playerid)return SendClientMessage(playerid,0xFF9900AA,"You Can't Give Money To Yourself!");
    else
    {
        GivePlayerMoney(giveplayerid, amount);
        GameTextForPlayer(giveplayerid,"~g~Money Recived",3000,5);
        format(string, sizeof(string), "You Recived $%d From Player %s(%d)",amount,playername,giveplayerid);
        SendClientMessage(giveplayerid,0x33CCFFAA,string);
       
        GivePlayerMoney(playerid, 0 - amount);
        GameTextForPlayer(playerid,"~y~Money Sent",3000,5);
        format(string, sizeof(string), "You Have Sent $%d To Player %s(%d)",amount,playername,playerid);
        SendClientMessage(playerid,0x33CCFFAA,string);
    }
    return 1;
}
You just needed to use "return SendClientMessage" instead of "SendClientMessage", so it won't go further down the code. That's all.

Cheers.
Reply
#4

Quote:
Originally Posted by DeathOnaStick
Quote:
Originally Posted by mprofitt
pawn Код:
dcmd_givecash(playerid, params[])
{
    if (sscanf(params, "ud", giveplayerid, amount)) return SendClientMessage(playerid, 0xFF9900AA, "Usage: /givecash [playerid/partname] [amount]");
   
    new
        string[256],
        playername[MAX_PLAYER_NAME],
        giveplayerid,
        amount;
   
    if (giveplayerid == INVALID_PLAYER_ID) SendClientMessage(playerid, 0xFF9900AA, "Player Not Found");
    else if (amount > GetPlayerMoney(playerid)) SendClientMessage(playerid, 0xFF9900AA, "You Don't Have Enough Money!");
    else if (giveplayerid == playerid) SendClientMessage(playerid,0xFF9900AA,"You Can't Give Money To Yourself!");
    else
    {
        GetPlayerName(playerid, playername, MAX_PLAYER_NAME);
        GivePlayerMoney(giveplayerid, amount);
        GameTextForPlayer(giveplayerid,"~g~Money Recived",3000,5);
        format(string, sizeof(string), "You Recived $%d From Player %s(%d)",amount,playername,giveplayerid);
        SendClientMessage(giveplayerid,0x33CCFFAA,string);
       
        GivePlayerMoney(playerid, 0 - amount);
        GameTextForPlayer(playerid,"~y~Money Sent",3000,5);
        format(string, sizeof(string), "You Have Sent $%d To Player %s(%d)",amount,playername,playerid);
        SendClientMessage(playerid,0x33CCFFAA,string);
    }
    return 1;
}
Fail. Use this:
pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
    dcmd(givecash, 8, cmdtext);
    return 0;
}

dcmd_givecash(playerid, params[])
{
  new string[256];
  new playername[MAX_PLAYER_NAME];
    GetPlayerName(playerid, playername, MAX_PLAYER_NAME);
    new
        giveplayerid,
        amount;
    if (sscanf(params, "ud", giveplayerid, amount))return SendClientMessage(playerid, 0xFF9900AA, "Usage: /givecash [playerid/partname] [amount]");
    else if (giveplayerid == INVALID_PLAYER_ID)return SendClientMessage(playerid, 0xFF9900AA, "Player Not Found");
    else if (amount > GetPlayerMoney(playerid))return SendClientMessage(playerid, 0xFF9900AA, "You Don't Have Enough Money!");
    else if (giveplayerid == playerid)return SendClientMessage(playerid,0xFF9900AA,"You Can't Give Money To Yourself!");
    else
    {
        GivePlayerMoney(giveplayerid, amount);
        GameTextForPlayer(giveplayerid,"~g~Money Recived",3000,5);
        format(string, sizeof(string), "You Recived $%d From Player %s(%d)",amount,playername,giveplayerid);
        SendClientMessage(giveplayerid,0x33CCFFAA,string);
       
        GivePlayerMoney(playerid, 0 - amount);
        GameTextForPlayer(playerid,"~y~Money Sent",3000,5);
        format(string, sizeof(string), "You Have Sent $%d To Player %s(%d)",amount,playername,playerid);
        SendClientMessage(playerid,0x33CCFFAA,string);
    }
    return 1;
}
You just needed to use "return SendClientMessage" instead of "SendClientMessage", so it won't go further down the code. That's all.

Cheers.
LMAO..
you /failed harder

Cheers
Reply
#5

Bah. Tell me why. Whats wrong. In my opinion everythings alright.
Reply
#6

In mprofitt's example, if you use else ifs and one if is true then there is no possible way the others can be executed
Reply
#7

Quote:
Originally Posted by dice7
In mprofitt's example, if you use else ifs and one if is true then there is no possible way the others can be executed
Lol he edited . I dont understand anything anymore XD...
Reply
#8

Quote:
Originally Posted by DeathOnaStick
Quote:
Originally Posted by dice7
In mprofitt's example, if you use else ifs and one if is true then there is no possible way the others can be executed
Lol he edited . I dont understand anything anymore XD...
I was referring to the code you quoted from him. And both codes are correct
Reply
#9

Use ZCMD + Sscanf..

Here's a "shell":

pawn Код:
cmd(givemoney, playerid, params[])
{
new user, amount, string[128];
if(!sscanf(params, "ui", user, amount))
{
//do your code here.
}
}
Reply
#10

sscanf will fail if it doesn't have the required "space". You use the variables before defining them (mprofitt). That's fail. On a related note, if you meet any of the "bad" cases you want to skip the rest of the execution. Basically, use return. You wouldn't want to give a player money after you just told him he couldn't would you?
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)