Hide function

Hello,

Ive got some problems.
What do i need to do for these problems:

I want so my health bar only appears when a player is under 100 health. (it will slide down when the player is back up to 100 health)
I want so my armor bar only appears when it gains armor or if the player is under 100 health. (it will slide down when the player is back up to 100 health )

I want so the player gains health when he didn’t get hit or didn’t got damage for 5 seconds. (the healthbar appears when he gains health)

Does someone have any helpfull links or maybe give a quick tut?

Thank you!


local health -- declare it in the main chunk, no need to make a global
local time = 0

hook.Add("HUDPaint", "HUD", function()
	local client = LocalPlayer() -- cache LocalPlayer, you should only call it once.
	local curHealth = client:Health() -- get the client's current health

	if (health == curHealth) then -- start the timer
		if (time ~= 0) then
			if (CurTime() > time) then
				return -- stops the rest of the function running
			end
		else -- start the timer
			time = CurTime() + 3
		end
	else -- reset timer and health
		health = curHealth
		time = 0
	end

	-- draw your HUD here
	draw.SimpleText(curHealth, "DermaDefault", 100, 100, color_black)
end)


Works like a charm!
One question tho… How would i do the health regen? So the player gains health when he didn’t got damage for 5 seconds?
And when it dissapears and shows again that it’s going in screen with a fade not just pop up.

You could use

surface.SetAlphaMultiplier for the fading.

And the health regen?

You should really learn how to use Google.
http://forum.facepunch.com/showthread.php?t=1059592&p=28035145&viewfull=1#post28035145

Yes, that’s for crouching. But how should i apply the damage thing? Sorry for doing dumb but im trying to learn.

You can easily adapt that code snippet to what you want. Read it and at least try to understand it.

Okay, i will try.

[editline]5th September 2017[/editline]

I understand it but the regen thing doesn’t work. As soon as a crouch nothing happends. So i dont think it will work if i put the damage code in.

I created something similar for another guy some time ago:



local teams;

hook.Add("Initialize", "initializeTeams", function()
    teams = {
        [TEAM_CITIZEN] = 20,
        [TEAM_MOB] = 5,
        [TEAM_MEDIC] = 30
    };
end);

hook.Add("EntityTakeDamage", "resetCooldown", function(ent, dmg)
    if (not IsValid(ent) or not ent:IsPlayer()) then return; end

    ent.nextRegen = CurTime() + 5;
end);

local function ShouldRegen(ply)
    if (not ply.nextRegen) then return false end;
    if (ply.nextRegen > CurTime()) then return false end;
    if (not ply:Alive()) then return false end;
    if (ply:Health() == ply:GetMaxHealth()) then return false end;
    if (not teams[ply:Team()]) then return false end;

    return true;
end

local function GiveHealth(ply)
    ply:SetHealth(math.Clamp(ply:Health() + teams[ply:Team()], 0, ply:GetMaxHealth()));

    ply.nextRegen = CurTime() + 5;
end

hook.Add("Tick", "regenHealth", function()
    local playerCount = player.GetCount();
    local players     = player.GetAll();
    
    for i = 1, playerCount do
        local Player = players*;
        if (not IsValid(Player) or not ShouldRegen(Player)) then continue; end
        
        GiveHealth(Player);
    end
end);


It is DarkRP job based but you can easily make it not.

Hey, thanks man!
Should this work?


  hook.Add("EntityTakeDamage", "resetCooldown", function(ent, dmg)
    if (not IsValid(ent) or not ent:IsPlayer()) then return; end

    ent.nextRegen = CurTime() + 5;
end);

local function ShouldRegen(ply)
    if (not ply.nextRegen) then return false end;
    if (ply.nextRegen > CurTime()) then return false end;
    if (not ply:Alive()) then return false end;
    if (ply:Health() == ply:GetMaxHealth()) then return false end;

    return true;
end

local function GiveHealth(ply)
    ply:SetHealth(math.Clamp(ply:Health(), 0, ply:GetMaxHealth()));

    ply.nextRegen = CurTime() + 5;
end

hook.Add("Tick", "regenHealth", function()
    local playerCount = player.GetCount();
    local players     = player.GetAll();
    
    for i = 1, playerCount do
        local Player = players*;
        if (not IsValid(Player) or not ShouldRegen(Player)) then continue; end
        
        GiveHealth(Player);
    end
end); 

No, but this should.



hook.Add("EntityTakeDamage", "resetCooldown", function(ent, dmg)
    if (not IsValid(ent) or not ent:IsPlayer()) then return; end

    ent.nextRegen = CurTime() + 5;
end);

local function ShouldRegen(ply)
    if (not ply.nextRegen) then return false end;
    if (ply.nextRegen > CurTime()) then return false end;
    if (not ply:Alive()) then return false end;
    if (ply:Health() == ply:GetMaxHealth()) then return false end;

    return true;
end

local function GiveHealth(ply)
    ply:SetHealth(math.Clamp(ply:Health() + 5, 0, ply:GetMaxHealth()));

    ply.nextRegen = CurTime() + 5;
end

hook.Add("Tick", "regenHealth", function()
    local playerCount = player.GetCount();
    local players     = player.GetAll();
    
    for i = 1, playerCount do
        local Player = players*;
        if (not IsValid(Player) or not ShouldRegen(Player)) then continue; end
        
        GiveHealth(Player);
    end
end);


Does not work /:

It does.

No, it doesn’t do anything at all? Anything else that i can use?

My apologies good sir, the script ONLY works for me it seems, literally no one else in the world can make this !ADVANCED! piece of code work!!