Using if GetPlayerMoney
#1

Hey, I can't remember how I did this properly last time, but basically I've created a little gun store dialog so when a player types /gunshop it opens a dialog and they can select either legal or illegal weapons. When the player goes to buy a gun (clicks on the gun), I would like it to get the players money and if they don't have enough for the gun... send the player a message saying "You don't have enough money" etc. I wasn't sure where to put the if(GetPlayerMoney(playerid)) so if you do have enough money, it continues to give you the weapon an take your money. Sorry if I explained this shitty, haven't had much sleep haha.

Here's the dialog:

Код:
if(dialogid == 126)
	{
		if(response)
		{
			if(listitem == 0)
			{
  				GivePlayerWeapon(playerid, 24, 9999999);
				GivePlayerMoney(playerid, -15000);
			}

			if(listitem == 1)
			{
			    GivePlayerWeapon(playerid, 29, 9999999);
			    GivePlayerMoney(playerid, -3000);
			    return 1;
			}
			if(listitem == 2)
			{
			    GivePlayerWeapon(playerid, 25, 9999999);
			    GivePlayerMoney(playerid, -1500);
			    return 1;
			}
			if(listitem == 3)
			{
			    GivePlayerWeapon(playerid, 22, 9999999);
			    GivePlayerMoney(playerid, -750);
			    return 1;
			}
			if(listitem == 4)
			{
			    GivePlayerWeapon(playerid, 23, 9999999);
			    GivePlayerMoney(playerid, -1000);
				return 1;
			}
			if(listitem == 5)
			{
			    GivePlayerWeapon(playerid, 28, 9999999);
			    GivePlayerMoney(playerid, -25000);
			    return 1;
			}
			if(listitem == 6)
			{
			    GivePlayerWeapon(playerid, 32, 9999999);
			    GivePlayerMoney(playerid, -25000);
				return 1;
			}
			if(listitem == 7)
			{
			    GivePlayerWeapon(playerid, 33, 9999999);
			    GivePlayerMoney(playerid, -75000);
			    return 1;
			}
			if(listitem == 8)
			{
			    GivePlayerWeapon(playerid, 4, 9999999);
			    GivePlayerMoney(playerid, -1000000);
			    return 1;
			}
	    }
	}
Reply
#2

Example:
pawn Код:
if(dialogid == 126)
{
    if(response)
    {
        if(listitem == 0)
        {
            if(GetPlayerMoney(playerid) < 15000) return SendClientMessage(playerid, 0xFFFFFF00, "You don't have enough money to purchase this weapon.");
            GivePlayerWeapon(playerid, 24, 9999999);
            GivePlayerMoney(playerid, -15000);
        }
        // ...
Reply
#3

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
Example:
pawn Код:
if(dialogid == 126)
{
    if(response)
    {
        if(listitem == 0)
        {
            if(GetPlayerMoney(playerid) < 15000) return SendClientMessage(playerid, 0xFFFFFF00, "You don't have enough money to purchase this weapon.");
            GivePlayerWeapon(playerid, 24, 9999999);
            GivePlayerMoney(playerid, -15000);
        }
        // ...
I think that would return them the "you don't have enough money" message but it would still give them the weapon and take their money.

I fixed it with this though:

Код:
if(dialogid == 126)
	{
		if(response)
		{
			if(listitem == 0)
			{
				GetPlayerMoney(playerid);
				if(GetPlayerMoney(playerid) <15000)
				SendClientMessage(playerid, 0xffffff, "You do not have enough money to buy this weapon!");
				else
			{
  				GivePlayerWeapon(playerid, 24, 9999999);
				GivePlayerMoney(playerid, -15000);
				return 1;
			}
		}
Reply
#4

Quote:
Originally Posted by Josh_Main
Посмотреть сообщение
I think that would return them the "you don't have enough money" message but it would still give them the weapon and take their money.
The code that was given to you should work perfectly. Since there is a return, the code will stop there. This means that anything after it will not take place. It also takes up less lines and, in my opinion, it's easier to read.
Reply
#5

This is on how to neaten up the code a little, also returns values. You could state of how much you need.

Код:
	    if(dialogid == 126)
	    {
	        if(response)
	        {
				switch(listitem)
				{
				    case 0: // Desert Eagle
				    {
						if(GetPlayerMoney(playerid) > 4000)
						{
						    GivePlayerMoney(playerid, -4000);
						    GivePlayerWeapon(playerid, 24, 999999);
						    SendClientMessage(playerid, COLOR_WHITE, "Desert Eagle Bought.");
						}
						else return SendClientMessage(playerid, 0xFFFFFF, "You do not have enough money.");
				    }
				    
				    case 1: // Shotgun
				    {
				        if(GetPlayerMoney(playerid) > 6000)
						{
						    GivePlayerMoney(playerid, -6000);
						    GivePlayerWeapon(playerid, 25, 999999);
						    SendClientMessage(playerid, COLOR_WHITE, "Shotgun Bought.");
						}
						else return SendClientMessage(playerid, 0xFFFFFF, "You do not have enough money.");
				    }
				    
				    case 2: // Sniper Rifle
				    {
				        if(GetPlayerMoney(playerid) > 15000)
						{
						    GivePlayerMoney(playerid, -15000);
						    GivePlayerWeapon(playerid, 34, 999999);
						    SendClientMessage(playerid, COLOR_WHITE, "Sniper Bought Bought.");
						}
						else return SendClientMessage(playerid, 0xFFFFFF, "You do not have enough money.");
				    }
				}
			}
		}
Reply
#6

Quote:
Originally Posted by NRG2
Посмотреть сообщение
This is on how to neaten up the code a little, also returns values. You could state of how much you need.

Код:
	    if(dialogid == 126)
	    {
	        if(response)
	        {
				switch(listitem)
				{
				    case 0: // Desert Eagle
				    {
						if(GetPlayerMoney(playerid) > 4000)
						{
						    GivePlayerMoney(playerid, -4000);
						    GivePlayerWeapon(playerid, 24, 999999);
						    SendClientMessage(playerid, COLOR_WHITE, "Desert Eagle Bought.");
						}
						else return SendClientMessage(playerid, 0xFFFFFF, "You do not have enough money.");
				    }
				    
				    case 1: // Shotgun
				    {
				        if(GetPlayerMoney(playerid) > 6000)
						{
						    GivePlayerMoney(playerid, -6000);
						    GivePlayerWeapon(playerid, 25, 999999);
						    SendClientMessage(playerid, COLOR_WHITE, "Shotgun Bought.");
						}
						else return SendClientMessage(playerid, 0xFFFFFF, "You do not have enough money.");
				    }
				    
				    case 2: // Sniper Rifle
				    {
				        if(GetPlayerMoney(playerid) > 15000)
						{
						    GivePlayerMoney(playerid, -15000);
						    GivePlayerWeapon(playerid, 34, 999999);
						    SendClientMessage(playerid, COLOR_WHITE, "Sniper Bought Bought.");
						}
						else return SendClientMessage(playerid, 0xFFFFFF, "You do not have enough money.");
				    }
				}
			}
		}
That's not neat at all, you're using more lines than necessary.

pawn Код:
if(GetPlayerMoney(playerid) < 15000) return SendClientMessage(playerid, 0xFFFFFF00, "You don't have enough money to purchase this weapon.");
// ...
pawn Код:
if(GetPlayerMoney(playerid) > 15000)
{
    // ...
}
else return SendClientMessage(playerid, 0xFFFFFF, "You do not have enough money.");
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)