HUD Question

Okay so is there a code for the max health on the hud, becuase right now i get this when i get over 100 hp

Code would help. You should be doing:

width * (current health / max health)

To get the filled width of the healthbar.


surface.DrawRect(0, 0, 1000 * (LocalPlayer():Health() / LocalPlayer():GetMaxHealth()), 100)


Will make a rectangle on coordinate 0;0 that is 100 pixels tall and depending on the health will be 1000 pixels long if it’s maxed out

math.Clamp(ply:Health(), 0, ply:GetMaxHealth())

You should do that so that it never ends up exceeding their max health (by default, 100), you can draw the HUD as if it was 100.

I don’t think the other two quite understood what the problem was, as both those “solutions” would still end up with drawing a rectangle larger than the max if there was over max health.

It wont work if health maximum is set to 10000 and he wants to draw 100 pixels wide box
OP, just take the first solution

I’m working on the assumption that 1: He’s not an idiot and 2: Whatever thing he’s using to get the width is using / max health rather than / 100 3: He’s probably NOT going to have a max health of over 100 and if he planned on it he would plan with that in mind 4:HE’S NOT AN IDIOT

[editline]28th October 2017[/editline]

First solution breaks if they have over 100 health, that completely avoids his question at all and is doing nothing other than trying to rewrite whatever code he has.

I’m not saying to use the math.Clamp as the sole way to get the width, but to use it in place of ply:Health() in, for example, the first “solution”

[editline]28th October 2017[/editline]

Y’all are completely ignoring his actual problem and just trying to rewrite code that would cause it.

It’s not

That completely ignores the problem, if the player has over the max health, for example, 200 for 100, the health bar would still have the EXACT SAME PROBLEM and would be twice the width of what it should be

[editline]28th October 2017[/editline]

For Example: Player has 200 Health. Player’s max health is 100. Width is 500.

500 * (200/100)
500 * 2
1000 would be the width of the health bar.
That is the EXACT problem that is shown in the picture. The code is simply reproducing the problem not fixing it

what the fuck are you gobbling on about

Read the most recent post, it explains exactly what OP’s problem is and explains how you are doing nothing but reproducing it

[editline]28th October 2017[/editline]

What, would you like to explain how it doesn’t do exactly what the OP is describing? Because all I can see is that by all common knowledge that is probably the exact code OP has right now.

couldn’t you just check to make sure the health bar doesn’t draw past a certain length if the players health is above 100? It probably won’t scale based on your health properly but it’s a simple solution.

You know what else is a simple solution?

width * (math.Clamp(ply:Health(), 0, ply:GetMaxHealth()) / ply:GetMaxHealth())

That’s exactly what I posted with an unnecessary math.Clamp call.

You don’t need clamp because your health doesn’t go over your max health :-I
If you find scripts that put your health over the max it’s because they change the Max Health right before setting the HP so it will still work.

I Don’t understand how to use that one sorry, i’m a bit new :stuck_out_tongue:

Also the other ones dosen’t work

it’d help if you posted the code, nobody can help if they don’t know what you’re trying to do. bonus points for using CODE brackets

Yeah and please don’t comment how bad it is, first time :stuck_out_tongue:

Please don’t create vgui elements in a HUDPaint hook or use draw.RoundedBox with 0 as the first argument.

local function DrawHUD()
    local client = LocalPlayer()
    local health, maxHealth = client:Health(), client:GetMaxHealth()
    -- healthbar background
    surface.DrawRect(5, ScrH() - 25, 300, 20)
    -- actual healthbar
    surface.DrawRect(5, ScrH() - 25, 300 * (health / maxHealth), 20)
hook.Add("HUDPaint", "hud", DrawHUD)

Okay thanks, just a question, why do i have to do that like can it break the hud or?

[editline]28th October 2017[/editline]

That one just gives me some whitebar that dosent work either…

For me monkeymacman’s code works:

local health = math.Clamp( ply:Health(), 0, ply:GetMaxHealth() )

Here, I tried to fix everything:

local avatar

hook.Add( "HUDPaint", "MyNameJeff", function()

	local ply = LocalPlayer()

	local health = math.Clamp( ply:Health(), 0, ply:GetMaxHealth() )
	surface.SetDrawColor( 13, 13, 13, 275 )
	surface.DrawRect( 5, 925, 310, 150 )
	surface.SetDrawColor( 40, 40, 40 )
	surface.DrawRect( 8, ScrH() - 43, 304, 34 )
	surface.SetDrawColor( 255, 0, 0 )
	surface.DrawRect( 10, ScrH() - 41, health * 3, 30 )

	local armor = ply:Armor()
	surface.SetDrawColor( 40, 40, 40 )
	surface.DrawRect( 8, ScrH() - 80, 304, 33 )
	surface.SetDrawColor( 0, 0, 255 )
	surface.DrawRect( 10, ScrH() - 78, armor * 3, 30 )

	if not IsValid( avatar ) then
		avatar = vgui.Create( "AvatarImage" )
		avatar:SetSize( 64, 64 )
		avatar:SetPos( 10, 930 )
		avatar:SetPlayer( ply, 64 )

	draw.DrawText( ply:Nick(), "DarkRPHUD2", ScrW() - 1830, ScrH() - 132, color_white )

	surface.SetDrawColor( 13, 13, 13, 275 )
	surface.DrawRect( 5, 882, 310, 35 )
	draw.DrawText( "$" .. ply:getDarkRPVar( "money" ), "DarkRPHUD2", ScrW() - 1915, ScrH() - 191, color_white )

end )