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
Screenshot: [url]https://gyazo.com/196c73bf998202d15fb8bc00c596c889[/url]
Code would help. You should be doing:
[code]width * (current health / max health)[/code]
To get the filled width of the healthbar.
[lua]
surface.DrawRect(0, 0, 1000 * (LocalPlayer():Health() / LocalPlayer():GetMaxHealth()), 100)
[/lua]
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
[code]math.Clamp(ply:Health(), 0, ply:GetMaxHealth())[/code]
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.
[QUOTE=monkeymacman;52830615][code]math.Clamp(ply:Health(), 0, ply:GetMaxHealth())[/code]
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.[/QUOTE]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
[QUOTE=LoweChaser;52830620]It wont work if health maximum is set to 10000 and he wants to draw 100 pixels wide box[/QUOTE]
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]
[QUOTE=LoweChaser;52830620]
OP, just take the first solution[/QUOTE]
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.
[QUOTE=monkeymacman;52830627]First solution breaks if they have over 100 health.[/QUOTE]It's not
[QUOTE=txike;52830530]Code would help. You should be doing:
[code]width * (current health / max health)[/code]
To get the filled width of the healthbar.[/QUOTE]
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]
[QUOTE=LoweChaser;52830635]It's not[/QUOTE]
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
[QUOTE=txike;52830643]what the fuck are you gobbling on about[/QUOTE]
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?
[code]width * (math.Clamp(ply:Health(), 0, ply:GetMaxHealth()) / ply:GetMaxHealth())[/code]
[QUOTE=monkeymacman;52830851]You know what else is a simple solution?
[code]width * (math.Clamp(ply:Health(), 0, ply:GetMaxHealth()) / ply:GetMaxHealth())[/code][/QUOTE]
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.
[QUOTE=txike;52830530]Code would help. You should be doing:
[code]width * (current health / max health)[/code]
To get the filled width of the healthbar.[/QUOTE]
I Don't understand how to use that one sorry, i'm a bit new :P
Also the other ones dosen't work
[QUOTE=s00L;52831021]I Don't understand how to use that one sorry, i'm a bit new :P
Also the other ones dosen't work[/QUOTE]
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
[QUOTE]hook.Add( "HUDPaint", "HUDPaint_DrawABox", function()
local health = LocalPlayer():Health()
draw.RoundedBox(0,5,925,310,150,Color(13,13,13, 275))
draw.RoundedBox(0,8,ScrH() - 43,300+4 , 30 + 4,Color(40,40,40))
draw.RoundedBox(0,10,ScrH() - 41 ,health * 3,30,Color(255,0,0))
local armor = LocalPlayer():Armor()
draw.RoundedBox(0,8,ScrH() - 80,300+4 , 3 + 30,Color(40,40,40))
draw.RoundedBox(0,10,ScrH() - 78 ,armor * 3,30,Color(0,0,255))
local Avatar = vgui.Create( "AvatarImage", Panel )
Avatar:SetSize( 64, 64 )
Avatar:SetPos( 10, 930 )
Avatar:SetPlayer( LocalPlayer(), 64 )
draw.DrawText( LocalPlayer():Nick(), "DarkRPHUD2", ScrW() - 1825 + 5, ScrH () - 130 + 2, Color (255,255,255,255))
draw.RoundedBox(0,5,882,310,35,Color(13,13,13, 275))
draw.DrawText( "$"..LocalPlayer():getDarkRPVar( "money" ), "DarkRPHUD2", ScrW() - 1910 + 5, ScrH () - 190 + 1, Color(255,255,255))
end )[/QUOTE]
Yeah and please don't comment how bad it is, first time :P
Please don't create vgui elements in a HUDPaint hook or use draw.RoundedBox with 0 as the first argument.
[code]local function DrawHUD()
local client = LocalPlayer()
local health, maxHealth = client:Health(), client:GetMaxHealth()
-- healthbar background
surface.SetDrawColor(color_black)
surface.DrawRect(5, ScrH() - 25, 300, 20)
-- actual healthbar
surface.SetDrawColor(color_white)
surface.DrawRect(5, ScrH() - 25, 300 * (health / maxHealth), 20)
end
hook.Add("HUDPaint", "hud", DrawHUD)
[/code]
Okay thanks, just a question, why do i have to do that like can it break the hud or?
[editline]28th October 2017[/editline]
[QUOTE=txike;52831109]Please don't create vgui elements in a HUDPaint hook or use draw.RoundedBox with 0 as the first argument.
[code]local function DrawHUD()
local client = LocalPlayer()
local health, maxHealth = client:Health(), client:GetMaxHealth()
-- healthbar background
surface.SetDrawColor(color_black)
surface.DrawRect(5, ScrH() - 25, 300, 20)
-- actual healthbar
surface.SetDrawColor(color_white)
surface.DrawRect(5, ScrH() - 25, 300 * (health / maxHealth), 20)
end
hook.Add("HUDPaint", "hud", DrawHUD)
[/code][/QUOTE]
That one just gives me some whitebar that dosent work either..
Nevermind. My screen's too small. The rest of the code should be slightly less laggy though
[QUOTE=s00L;52831186]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..[/QUOTE]
It's working fine for me.
[IMG]https://i.imgur.com/b1DOv9Z.jpg[/IMG]
[QUOTE=txike;52831561]It's working fine for me.[/QUOTE]
Try eating a couple of bouncy ball entities. It breaks for me.
[t]https://i.imgur.com/ENn1Akx.png[/t]
[QUOTE=MPan1;52831570]Try eating a couple of bouncy ball entities. It breaks for me.[/QUOTE]
[code]math.min(300, 300 * (health / maxHealth))[/code]
fixed very hard OP totally couldn't have figured that out on his own.
:snip: automerge
[editline]28th October 2017[/editline]
I guess this would work then:
[CODE]
local avatar
hook.Add( "HUDPaint", "MyNameJeff", function()
local ply = LocalPlayer()
local health = math.Clamp( ply:Health() / ply:GetMaxHealth(), 0, 1 )
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 * 900, 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 )
end
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 )
[/CODE]
[B]EDIT:[/B] Updated, should work now
Poor s00l, he just wanted to know a basic function and he even mentioned that he's new, yet everyone's having a war, I think I atleast owe him an explanation on what some of these functions do so he can do it himself in the future.
[lua]
LocalPlayer() //You probably already know, it gets the player entity of you. (The person viewing the HUD)
math.Clamp(x1,x2,x3) //x2 is the minimum x1 will go, x3 is the maximum x1 will go. If you put math.Clamp(150, 0, 100) it will return 100 because 150 goes over the max of 100
math.min(x1,x2) //Basically math.Clamp but without a max
math.max(x1,x2) //Opposite of math.min, no minimum but there's a max
:Health() //Will return the health of the player
:GetMaxHealth() //Will return the max health of a player, if max health is set at 150 (Normally it's 100) when you use a medkit it will heal you up to 150hp then stop
[/lua]
I will correct myself, you can set your health over your Max Health using lua, so I'm sorry if that confused you.
After reading this I hope you see that Moat's response is the best one, he is setting some variables to make it easier, all you have to do is to change "x", "y", "max_width" and "height" yourself.
[QUOTE=Ubre;52831013]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.[/QUOTE]
your health can go below 0 tho
[lua]
local max = LocalPlayer():GetMaxHealth() //Get the player's maximum health (usually 100)
local hp = math.Clamp(LocalPlayer():Health(), 0, max) //Clamp it so that it cannot go above the max, or below 0 (to avoid the bar going backwards or overflowing)
local barWidth, barHeight = 150, 30 //Define variables for your health bar's width and height (preferrable outside your draw function but this is for demonstration purposes)
surface.SetDrawColor(255, 255, 255, 255) //Set the color to white, this will be the background
surface.DrawRect(0, 0, barWidth, barHeight) //Draws the background bar at position 0, 0 with width of 150 and height of 30 (barWidth, barHeight above)
surface.SetDrawColor(255, 0, 0, 255) //Set the color to red for our actual health bar
/*
hp/max returns a number between 0-1 which says which percentage of health the player is at.
1 for 100%, 0.3 for 30%, etc.
If you multiply the total width of the bar (barWidth) by this decimal, you will get a width relative to the player's health.
For example: barWidth*0.6 (hp/max -> 60/100 hp) will return 90, assuming barWidth is 150. 90 is 60% of 150.
There, you got a health bar.
*/
surface.DrawRect(0, 0, barWidth*hp/max, barHeight)
[/lua]
Does this not work?
[CODE]
local health = math.Clamp( ply:Health() / ply:GetMaxHealth(), 0, 1 ) * barWidth
[/CODE]
[editline]30th October 2017[/editline]
I'm pretty sure the code in my last post works now and does what you want
Sorry, you need to Log In to post a reply to this thread.