Player Health problem
#1

I have a trouble with health float and a float variable. For example I want to make serversided health.

Here's my code which i use

pawn Code:
enum PlayerInfo
{
    Float:pHealth,
    Float:pArmor,
}
new pInfo[MAX_PLAYERS][PlayerInfo];


new Float:Health, Float:Armor;
GetPlayerHealth(playerid, Health);
GetPlayerArmour(playerid, Armor);
pInfo[playerid][pHealth] = Health;
pInfo[playerid][pArmor] = Armor;
So what I am trying to do here is to set players current HP into server sided HP, but unfortunately it doesn't work. Whenever I try to check my variable pInfo[playerid][pHealth] or pInfo[playerid][pArmor] they are 0 both they just don't change with my code. Any help would be appreciated.
Reply
#2

I'm not quite sure. Please try to remove , from after armor and try again.
Reply
#3

Code:
GetPlayerHealth(playerid, pInfo[playerid][pHealth]);
GetPlayerArmour(playerid, pInfo[playerid][pArmor]);
Reply
#4

Whenever you use SetPlayerHealth, use this instead.
pawn Code:
SetPlayerHealthEx(playerid, 90.0); //example
And add this stock to your script.

pawn Code:
stock SetPlayerHealthEx(playerid,Float:health)
{
    SetPlayerHealth(playerid,health);
    PlayerInfo[playerid][pHealth] = health;
    return 1;
}
This way, your serverside health will equal the clientside health.

Don't forget to make your OnPlayerTakeDamage callbacks and everything use the new stock.
Reply
#5

Can you give me an example how I use it on OnPlayerTakeDamage?
Reply
#6

Example:

pawn Code:
// reason = weaponid
// hp = hp to set
forward SetPlayerHealthEx(playerid,Float:hp,reason = 0);
stock SetPlayerHealthEx(playerid,Float:hp,reason = 0)
{
    new Float:oldhp = GetPlayerHealthEx(playerid);

    if(hp <= 0.0)
    {
        SetPlayerHealth(playerid,0);
        hp = 0.0;
    }

    if(reason != 18 && reason != 37)
        hp = float(floatround(hp,floatround_floor));

    SetPVarFloat(playerid,"ScriptHealth",hp);
    SetPlayerHealth(playerid, hp);
    return 1;
}

forward SetPlayerArmourEx(playerid,Float:armour,reason = 0);
stock SetPlayerArmourEx(playerid,Float:armour,reason = 0)
{
        if(armour < 0.0)
        armour = 0.0;

        if(reason != 18 && reason != 37)
            armour = float(floatround(armour,floatround_floor));

    SetPlayerArmour(playerid,armour);
    SetPVarFloat(playerid,"ScriptArmour",armour);
    return 1;
}

forward Float:GetPlayerHealthEx(playerid);
stock Float:GetPlayerHealthEx(playerid)
{
    return GetPVarFloat(playerid, "ScriptHealth");
}

forward Float:GetPlayerArmourEx(playerid);
stock Float:GetPlayerArmourEx(playerid)
{
    return GetPVarFloat(playerid, "ScriptArmour");
}




//playerid, amount of damage, weaponid of damage, current player HP, current player armor
forward DamagePlayer(playerid, Float:amount, weaponid, Float:hp, Float:armour);
public DamagePlayer(playerid, Float:amount, weaponid, Float:hp, Float:armour)
{
        if(weaponid == 41 || weaponid == 42) //spray or fire ex
        amount = amount-((25.0*amount)/100.0);

    if(weaponid != 54 && weaponid != 53)
    {
        if(armour > 0.0)
        {
                new Float:tmp = armour;
            tmp = tmp-amount;
                if(tmp < 0.0)
                {
                    amount = amount - armour;
                    armour = 0.0;
            }
            else
            {
                    armour = tmp;
                    amount = 0;
            }
            if(armour > GetPlayerArmourEx(playerid))
                armour = 0.0;
                   
            SetPlayerArmourEx(playerid, armour,weaponid);
        }
        hp = (hp - amount);
        if(hp > GetPlayerHealthEx(playerid))
            hp = 0.0;
               
        SetPlayerHealthEx(playerid, hp, weaponid);
    }
    else
    {
        hp = (hp - amount);
        if(hp > GetPlayerHealthEx(playerid))
            hp = 0.0;
            SetPlayerHealthEx(playerid, hp, weaponid);
    }
    return 1;
}


public OnPlayerTakeDamage(playerid, issuerid, Float: amount, weaponid, bodypart)
{          
    DamagePlayer(playerid, amount, weaponid, GetPlayerHealthEx(playerid), GetPlayerArmourEx(playerid));
    return 1;
}
Reply
#7

Why does that example is that big? Isn't it possible to do more easier way? Ohh and when I tried using this code in OnPlayerTakeDamage

pawn Code:
new Float:health;
      GetPlayerHealth(playerid, health);
      SetPlayerHealthEx(playerid, health-amount);
And whenever I jumped from a mid air loosing 5 HP so my clients HP subtracted to 95.000000, but script HP was 95.050003 why ? I need it be exactly same.
Reply
#8

Quote:
Originally Posted by Gilbonzo
View Post
Why does that example is that big? Isn't it possible to do more easier way? Ohh and when I tried using this code in OnPlayerTakeDamage

pawn Code:
new Float:health;
      GetPlayerHealth(playerid, health);
      SetPlayerHealthEx(playerid, health-amount);
And whenever I jumped from a mid air loosing 5 HP so my clients HP subtracted to 95.000000, but script HP was 95.050003 why ? I need it be exactly same.
Clientside HP is rounded up as stated by Y_Less; What you see in the client is 95.000000 but their actual HP is 95.050003; If you compare their current clientside and the scriptside HP they will be equal. Either ways this is unhackable. Even if the hacker sets their HP frozen at 100.0, once the scripted variable (ScriptHealth) reaches zero he will die, and if he had armor it will still bypass them.

If you want serverside HP/armor you need a way to control those values (Set/Get PlayerHealthEx/PlayerArmourEx); Plus that, DamagePlayer function has to make calculations for armor values. OnPlayerTakeDamage just tells you how many amount of damage the player has taken in general, not how many has the armor absorbed and how many went left to the HP. This script does that.

Doing what you just did in that example is not valid for armor. If the player who gets damaged has armor, it will get completely ignored by the script. Also, what you're doing lacks of need for a scripted health system, you're setting their client HP to their scripted HP and that makes the whole system senseless..

What this system does is basically track the damage of the player and then store it scriptwise. Players have a scripted health value that will decrease either by using the SetPlayerHealthEx function or at OnPlayerTakeDamage. If a hacker is shot while his HP frozen, OnPlayerTakeDamage will still be reported and the script will proceed to lower his HP. Once he takes enough damage, it will reach to zero and the script will proceed to kill him.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)