HUD Help

I need a bit of help with a hud.

How would i set it up where the HP bar would represent a percent of the MAXHP instead of a number
For example:
Say this is your HP bar ||||||||||

If your Max is 100 and your only at 50 this it would be around half, like so |||||
But if your Max was 1000 it would only be a small bit missing, like so |||||||||

Think of it like a pie chart. A circle, 360 degrees or 2pi radians, can represent millions of people while still being scaled to our screens. You just have to think of how you could display a ratio of current health to maximum health and then apply that to a finite space. If I have 1/2 of something, doesn’t that mean that even if the pie chart is printed to have the radius of the sun I’ll still need only half of the sun to represent my half? There you go.

So let’s say your health bar is 200px wide. Health is 1000HP.
The ratio of current health to max health would be 1000/1000 for full health (1).

So then just multiply 200px by the ratio of the health! You are essentially getting the necessary amount of 200px to satisfy representation of 1000HP, which would be 200px. You’re taking what you need from your model to represent the value. If someone only has 3/4 health, you only need 3/4 of those 200px.

Exactly, But i dont know where to start on doing this.

uh… let me try to make something similar to what you’re looking for. i’ll get back to you and show you where to start.


Anyone else Understand what i mean?

Do you already have a healthbar set up for, eg, 100 health?

If so, it shouldn’t be too hard to change it to a percentage instead of the actual health
Eg, change
local Hp = ply:GetHealth()
local Hp = (ply:GetHealth() / ply:GetMaxHealth())

Unless, of course, what you’re asking is how to make a health bar in the first place

Super basic health bar using the principle I brought up:
local function health_bar()
local maxhealth = 100
local curhealth = LocalPlayer():Health()
local maxwidth = 175
local realwidth = maxwidth * (curhealth / maxhealth)
local height = 20

if curhealth > 0 then
    draw.DrawText(tostring(curhealth), "ChatFont", 25 + realwidth, (ScrH() - 105), Color(255, 255, 255, 255), TEXT_ALIGN_LEFT)
    draw.RoundedBox(6, 25, ScrH() - 107.5, realwidth, height, Color(204, 0, 0, 175))


hook.Add(“HUDPaint”, “health_bar_example”, health_bar)

Thank you this is exactly what I meant

Just be careful when implementing it. The maxhealth should be set more appropriately, this was just demonstration. If someone’s health exceeds the max, the fraction becomes greater than 1 and the bar will grow.

The way I thought of countering this was to run a Think hook that constantly checks the LocalPlayer()'s health, and if it increases above the maxhealth, then the maxhealth is set to that. When you detect that the player is dead, reset the maxhealth to the base value that every player spawns with, and you should be golden.

Oh yea Ive already edited it in several ways to suit what im using it for :smiley: