health bar-hud

hey
i dont know why but my huds health bar always stays as full health and the health bars wont update his self for the new hp that the players got
(its not the full code, my full hud is with 467 lines)



local currentHealth = 0

local function DrawHUD( ply )
    local playerTeam = team.GetName( ply:Team() )
    local playerClass = player_manager.GetPlayerClass( ply )

    local prec = currentHealth, ply:GetMaxHealth()
    prec = math.Clamp( prec, 0, 1 )
    draw.RoundedBox( 0, 57, ScrH() - 150, 306 * prec, 15, Color(210,50,50,240) )

    surface.SetDrawColor( 255, 255, 255, 2 );
    surface.SetTexture( gradient );
    surface.DrawTexturedRect( 57, ScrH() - 145, 306, 10);

    surface.SetDrawColor( 0, 0, 0, 90 );
    surface.SetTexture( gradient2 );
    surface.DrawTexturedRect( 57, ScrH() - 150, 306, 15 );

		if (ply:Alive()) then

            DrawHUD( ply )

		end

function GM:HUDShouldDraw( name )
    if ( name == "CHudHealth" or name == "CHudAmmo" or name == "CHudSecondaryAmmo") then
        return false
    end

    return true
end

hook.Add( "Initialize", "Interp", function() 

    hook.Add( "Think", "InterpThink", function() 

        if LocalPlayer():Health() != currentHealth then
            currentHealth = Lerp( 0.10, currentHealth, LocalPlayer():Health() )
        end

    end )

end )


Are you calling DrawHUD in HUDPaint? I suspect you may not be, as you’re using a Think hook underneath, even though HUDPaint is called every frame and therefore makes it unnecessary

Pretty sure this is the problem:



local prec = currentHealth, ply:GetMaxHealth()
prec = math.Clamp( prec, 0, 1 )


Firstly, why are you setting one variable to be two values? This isn’t how you do it.

Secondly, why are you clamping the health between 0 and 1? ply:GetMaxHealth usually returns 100. You should be doing something like:



local prec = math.Clamp( currentHealth / ply:GetMaxHealth(), 0, ply:GetMaxHealth() )


That would result in the health bar being 30600 pixels long if he had full health (prec is a multiplier)



draw.RoundedBox( 0, 57, ScrH() - 150, 306 * prec, 15, Color(210,50,50,240) )


*hint: prec should be currentHealth/ply:GetMaxHealth() *

yea i dont use DrawHUD in HUDPaint. HUDPaint comes after the DrawHUD in my code
thats how its goes in my code DrawHUD>HUDPaint>Think
i will check that thanks

thanks but i dont think that its the problem in this case the math stuff is for the bar size

NOTE: the health bar is working but rarely