SA-MP Forums Archive
GetPlayerHealth problem - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: GetPlayerHealth problem (/showthread.php?tid=500181)



GetPlayerHealth problem - DarkLored - 11.03.2014

So Hi,
i made a hp limit of purchasing health so here is my problem


this is the code
pawn Код:
if(GetPlayerHealth(playerid,100))
             {
                SendClientMessage(playerid,COLOR_RED,"Your health is full you cannot purchase this item unless you lose some health");
                return 1;
             }
pawn Код:
LSCNR.pwn(2242) : error 035: argument type mismatch (argument 2)



Re: GetPlayerHealth problem - Matess - 11.03.2014

pawn Код:
new Float:gethp;
GetPlayerHealth(playerid,gethp)
if(gethp == 100.0)
             {
                SendClientMessage(playerid,COLOR_RED,"Your health is full you cannot purchase this item unless you lose some health");
                return 1;
             }



Re: GetPlayerHealth problem - JonathanFeitosa - 11.03.2014

pawn Код:
new Float:health = 100;
if(GetPlayerHealth(playerid, health))
{
        SendClientMessage(playerid, COLOR_RED,"Your health is full you cannot purchase this item unless you lose some health");
        return 1;
}



Re: GetPlayerHealth problem - PowerPC603 - 11.03.2014

Quote:
Originally Posted by JonathanFeitosa
Посмотреть сообщение
pawn Код:
new Float:health = 100;
if(GetPlayerHealth(playerid, health))
{
        SendClientMessage(playerid, COLOR_RED,"Your health is full you cannot purchase this item unless you lose some health");
        return 1;
}
First setting the health to 100.0, then overwriting it with GetPlayerHealth won't check if it's 100.0.
GetPlayerHealth gets the player's health and stores the value in the variable "health", overwriting your 100.0.
GetPlayerHealth doesn't return the health like this, it returns 1 if it was succesful and 0 if the player wasn't connected (failure).
https://sampwiki.blast.hk/wiki/GetPlayerHealth

Also checking if 100.0 is equal is quite inaccurate using floats.
pawn Код:
new Float:health;
GetPlayerHealth(playerid, health);
if (health >= 100.0)
{
        SetPlayerHealth(playerid, 100.0);
        SendClientMessage(playerid, COLOR_RED,"Your health is full you cannot purchase this item unless you lose some health");
        return 1;
}
When the health of a player is full, it might just be returned as 99.9999999 or 100.0000001, due to binary limitations to represent floating values accurately.

Just check if it's equal OR higher than 100.0 and update the health to 100.0 if this is the case.
It won't just do a check but also partially kill health-hacks as well to limit their health to 100.0.

Or to be adsolutely certain, use 99.9 for the check to be able to detect full health, even if the value would be 99.999998 or something like that:
pawn Код:
(if health >= 99.9)