Health bar!

I’m making a HUD for my StarwarsRP server and am having troubles with this one part.

On my server, health goes way over 100, meaning some jobs have 10,000HP.
I have made a health bar using the code below:


            local DrawHealth = LocalPlayer():Health() or 0
            local EchoHealth = LocalPlayer():Health() or 0

            if DrawHealth > 100 then DrawHealth = 100 end
            if DrawHealth < 0 then DrawHealth = 0 end

            if DrawHealth != 0 then
                draw.RoundedBox(7, 116, ScrH () - 57, (326-24) * DrawHealth / 100, 16, Color(255,70,70,80))
                draw.RoundedBox(7, 116, ScrH () - 55, (326-24) * DrawHealth / 100, 12, Color(255,70,70,170))
                draw.RoundedBox(5, 117, ScrH () - 54, (323-24) * DrawHealth / 100, 10, Color(255,255,255,70))
                draw.RoundedBox(3, 117, ScrH () - 49, (320-24) * DrawHealth / 100, 3, Color(255,50,50,255))
                draw.RoundedBox(3, 117, ScrH () - 52, (320-24) * DrawHealth / 100, 3, Color(220,108,108,255))

            end

How can I make it so my bar’s percentage in length is equal to the percentage of HP/total HP?

Thanks in advanced,
Dan

You can just do:




local p = math.Clamp( ply:Health()/ply:GetMaxHealth(), 0, 1 )

barWidth = somenumberorsmth*p 

draw.RoundedBox( 3, x, y, barWidth, height, color )


Isn’t seeming to work man, I got this:


p = math.Clamp( ply:Health()/ply:GetMaxHealth(), 0, 1 )
barWidth = (326-24) * p
draw.RoundedBox(7, 116, ScrH () - 57, barWidth, 16, Color(255,70,70,80))
draw.RoundedBox(7, 116, ScrH () - 55, barWidth, 12, Color(255,70,70,170))
draw.RoundedBox(5, 117, ScrH () - 54, barWidth, 10, Color(255,255,255,70))
draw.RoundedBox(3, 117, ScrH () - 49, barWidth, Color(255,50,50,255))
draw.RoundedBox(3, 117, ScrH () - 52, barWidth, Color(220,108,108,255))

I was just giving you an example, I didn’t mean implement it exactly. Make sure you’ve defined ply as LocalPlayer() and use different variables for the different bars (if they have different lengths).

The last to RoundedBox calls are missing the height argument as well.

comment what you have currently out, and place this below.


local DrawHealth = LocalPlayer():Health()

if DrawHealth > 100 then DrawHealth = 100 end
if DrawHealth < 0 then DrawHealth = 0 end

if DrawHealth != 0 then
	draw.RoundedBox(7, 116, ScrH () - 57, (326-24) * DrawHealth / LocalPlayer():GetMaxHealth(), 16, Color(255,70,70,80))
	draw.RoundedBox(7, 116, ScrH () - 55, (326-24) * DrawHealth / LocalPlayer():GetMaxHealth(), 12, Color(255,70,70,170))
	draw.RoundedBox(5, 117, ScrH () - 54, (323-24) * DrawHealth / LocalPlayer():GetMaxHealth(), 10, Color(255,255,255,70))
	draw.RoundedBox(3, 117, ScrH () - 49, (320-24) * DrawHealth / LocalPlayer():GetMaxHealth(), 3, Color(255,50,50,255))
	draw.RoundedBox(3, 117, ScrH () - 52, (320-24) * DrawHealth / LocalPlayer():GetMaxHealth(), 3, Color(220,108,108,255))
end

let me know if it works.

This just does the same as before, the health bar only goes down when the health is below 100.
I am setting the job’s HP in jobs.lua

[editline]20th February 2016[/editline]

I get you now, this does work but I need it so the bar’s length is dependant on the percentage of health the localplayer has left out of the total health the player spawned with (5000 HP in this case as it’s a Jedi Padawan job).

Any other answers would be appreciated big time.

Right now, I have this:


local ply = LocalPlayer()        
p = math.Clamp( ply:Health() / ply:GetMaxHealth(), 0, 1 )
barWidth = 302 * p
draw.RoundedBox(7, 116, ScrH () - 57, barWidth, 16, Color(255,70,70,80))
draw.RoundedBox(7, 116, ScrH () - 55, barWidth, 12, Color(255,70,70,170))
draw.RoundedBox(5, 117, ScrH () - 54, barWidth, 10, Color(255,255,255,70))
draw.RoundedBox(3, 117, ScrH () - 49, barWidth, 3, Color(255,50,50,255))
draw.RoundedBox(3, 117, ScrH () - 52, barWidth, 3, Color(220,108,108,255))


Of course the health bar is only going to go down if it’s below 100, you told it to:


            if DrawHealth > 100 then DrawHealth = 100 end
            if DrawHealth < 0 then DrawHealth = 0 end

TRANSLATION: If the health is above 100, just make it 100. If it’s less than 0 ( negative ), just make it 0.

Get rid of the first part of this and you’re gonna be fine.

It looks like it was added as a failsafe to prevent the bar from extending past the outline in it’s original use

[editline]20th February 2016[/editline]

If this is what you’re using now, then you must not be definining ply:GetMaxHealth(). By default, this will return 100 unless you set it otherwise. Use

Entity:SetMaxHealth to fix it up

Alrighty, but the issue being each job has different ammounts, is there a way to set the maxhealth to whatever the sethealth for the job in jobs.lua is?

I’m not exactly sure of the syntax, but there’s a PlayerLoadout parameter in the jobs table you could use. Have a look at the DarkRP Wiki for the exact way.

Been looking everywhere man, can’t find any solutions. D:

you guys have made this code worse omg

What? Then you do it, smart one.

sexy looking or just plain?

http://wiki.darkrp.com/index.php/DarkRP:CustomJobFields here’s all of them

What the fuck kind of question is that?

then you are stupid

Okay

good


(User was banned for this post ("Shitposting" - NiandraLades))