I'm working on a hud just for fun and I am having an issue with sizing the health bar. Everything is working as it should until I get to 17 health, once I pass 17 hp the bar is empty. I can't figure out what i'm doing wrong.
This is what the hud looks like at 17hp
https://i.imgur.com/b5SGtph.png
concommand.Add("ifuckedup",function()
hook.Remove("HUDPaint", "hud")
end)
local hide = {
["CHudHealth"] = true,
["CHudBattery"] = true,
["CHudAmmo"] = true,
["CHudCrosshair"] = true,
["CHudSecondaryAmmo"] = true
}
hook.Add( "HUDShouldDraw", "HideHUD", function( name )
if(hide[name]) then return false end
end )
oldhp = LocalPlayer():Health()
hook.Add("HUDPaint", "hud", function()
local plyHP = math.Clamp(LocalPlayer():Health(), 0, 100)
if (LocalPlayer():Health() ~= oldhp) then print(LocalPlayer():Health()) oldhp = LocalPlayer():Health() end
surface.SetDrawColor(109, 109, 109, 180) -- Light transparent grey
-- HP
surface.DrawPoly({{ x = 50, y = ScrH() - 150}, { x = 50, y = ScrH() - 130}, { x = 300, y = ScrH() - 130}, { x = 280, y = ScrH() - 150}})
--surface.DrawPoly({{ x = 50, y = ScrH() - 150}, { x = 50, y = ScrH() - 130}, { x = 300, y = ScrH() - 130}, { x = 280, y = ScrH() - 150}})
surface.SetDrawColor(255, 0, 0) -- Red
surface.DrawPoly({{ x = 50, y = ScrH() - 150}, { x = 50, y = ScrH() - 130}, { x = math.Clamp((plyHP / 100) * 300, 50, 300), y = ScrH() - 130}, { x = math.Clamp((plyHP / 100) * 280, 50, 280), y = ScrH() - 150}})
surface.SetDrawColor(109, 109, 109, 180) -- Light transparent grey
-- Armor
surface.DrawPoly({{ x = 50, y = ScrH() - 120}, { x = 50, y = ScrH() - 100}, { x = 320, y = ScrH() - 100}, { x = 300, y = ScrH() - 120}})
end)
I didn't check the position for the code .. but noted you're missing a material function.
surface.DrawPoly(data) render materials, just like surface.DrawTexturedRect(x,y,w,h).
You need draw.NoTexture() before surface.DrawPoly.
Not an answer to your question but you should clamp your health between LocalPlayer():Health() and LocalPlayer():GetMaxHealth().
You can't always be sure a player's max health will be 100.
I would probably just store the ratio as something like this.
local ratio = LocalPlayer():Health() / LocalPlayer():GetMaxHealth()
-- ... Code
surface.DrawPoly({..., ..., { x = math.Clamp(ratio * 300, 50, 300), y = ScrH() - 130 }, { x = math.Clamp(ratio * 280, 50, 280), y = ScrH() - 150 }})
-- ... More Code
Sorry, you need to Log In to post a reply to this thread.