Problem mixing up two things in a if function
#1

First of all, sorry if this question is pretty dumb but im using PAWN for just a few weeks (used to do it a long time ago) and want to do the following.
I want to check if a player has enough heal to use the /heal command, if it is above 50 it will use else.
There comes the problem when the player needs to heal himself the server first needs to check wether the player has enough money, but I also want to show him in the same line as I send him he doesn't have enough money his actual money and the money he needs.
So in short I want the sentence to show: You haven't got enough money to heal yourself (your money: x | needed: 1000)
This is my code for now:
Код:
	if (strcmp("/heal", cmdtext, true, 10) ==0)
	{
	new Float:Health;
	GetPlayerHealth(playerid, Health);
	if (Health < 50.0)
			{
		    SendClientMessage(playerid,0xD10000FF, "You are too low on health to heal yourself!");
            return 1;
			}
	else
	    	{
			if(GetPlayerMoney(playerid) < 1000)	return SendClientMessage(playerid,0xD10000FF ,"You haven't got enough money to heal yourself (Your money: $i% Needed: $1000)");
	    	SetPlayerHealth(playerid, 100);
	        SendClientMessage(playerid,0x009900FF,"You have healed yourself for $1000!");
	        GivePlayerMoney(playerid, -1000);
	        return 1;
			}

	}
Reply
#2

pawn Код:
if (strcmp("/heal", cmdtext, true, 10) ==0)
    {
    new Float:Health,str[128];
    GetPlayerHealth(playerid, Health);
    if (Health < 50.0)
            {
            SendClientMessage(playerid,0xD10000FF, "You are too low on health to heal yourself!");
            return 1;
            }
    else
            {
             if(GetPlayerMoney(playerid) < 1000)
                 {
                format(str,sizeof(str),"You haven't got enough money to heal yourself (Your money: $%i Needed: $1000)",GetPlayerMoney(playerid))
            return SendClientMessage(playerid,0xD10000FF ,str);
                }
            SetPlayerHealth(playerid, 100);
            SendClientMessage(playerid,0x009900FF,"You have healed yourself for $1000!");
            GivePlayerMoney(playerid, -1000);
            return 1;
            }

    }
Reply
#3

Test it not sure but I think it should work

pawn Код:
if (strcmp("/heal", cmdtext, true, 10) ==0)
    {
        new Float:Health, string[128], cash;
        cash = GetPlayerMoney(playerid);
       
        GetPlayerHealth(playerid, Health);
        if (Health < 50.0)
        {
            SendClientMessage(playerid,0xD10000FF, "You are too low on health to heal yourself!");
            return 1;
        }
        else
        {
            if(GetPlayerMoney(playerid) < 1000)
            {
                format(string, sizeof(string), "You haven't got enough money to heal yourself (Your money: $%i Needed: $1000)", cash);
                SendClientMessage(playerid,0xD10000FF ,string);
                return 1;
            }
            SetPlayerHealth(playerid, 100);
            SendClientMessage(playerid,0x009900FF,"You have healed yourself for $1000!");
            GivePlayerMoney(playerid, -1000);
            return 1;
        }
    }
Reply
#4

Yes, it would work. However, you're creating unnecessary variables where you don't need them, and too much cell size.

A shorter more efficient version would be:
pawn Код:
if(strcmp("/heal", cmdtext, true) == 0)
    {
        new Float:Health;
        GetPlayerHealth(playerid, Health);
        if(Health < 50.0) return SendClientMessage(playerid, 0xD10000FF, "You are too low on health to heal yourself!");
        new cash = GetPlayerMoney(playerid);
        if(cash < 1000)
        {
            new str[90];
            format(str, sizeof(str), "You haven't got enough money to heal yourself (Your money: $%d Needed: $1000)", cash);
            return SendClientMessage(playerid, 0xD10000FF, str);
        }
        SetPlayerHealth(playerid, 100.0);
        SendClientMessage(playerid, 0x009900FF, "You have healed yourself for $1000!");
        GivePlayerMoney(playerid, -1000);
        return 1;
    }
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)