Is there a way to seperate the healthbar?
For example: A bar that shows only 25% of the healthbar?
Thank you for your time and help?
(Yes, I'm new!)
Maaaybe set the size of the bar to
LocalPlayer():GetMaxHealth() * 0.25
You need to do math
The percent formula (or use similar funciton math.Remap)
Yeah, already tryed that out. But then I have the problem that it just creates 2 of the same bars, but one is just going to 50% and the other one to 75%.
make different percent formulas with clamping
I now understand what you mean, I tryed a code like this. But still won't work properly. I want so the first bar finishes at 25% and continue at a other bar that finishes at 50% and so on and on.
if LocalPlayer():Armor() <= 25 then
(Armorbar)
end
if LocalPlayer():Armor() >= 25 then
(Armorbar)
end
As I understand, you want two bars for health. One is from 0 to 25. Second is from 50 to 100.
Right?
For that ^ you can use something similar to this to get the width of each segment:
local armor = LocalPlayer():Armor()
local bar1Perc = math.Clamp(armor, 0, 25) / 25
local bar2Perc = math.Clamp(armor - 25, 0, 75) / 75
if (bar1Perc > 0) then
-- Draw the first portion of you bar here
if (bar2Perc > 0) then
-- Draw the second portion of your bar here, to the right of the first portion
end
end
Each of those will be a number from 0-1 that you multiply by the total width you want. bar1Perc is the player's progress from 0 to 25 armor, bar2Perc is the player's progress from 25-100 armor.
local ply = LocalPlayer()
if IsValid(ply) then -- always validate
local health, max_health = ply:Health(), ply:GetMaxHealth()
local split_into = 4 -- how many bars you're splitting the health into | important bit for u
local W, H = ScrW(), ScrH()
-- pixels per each bar | how many pixels does 1 health equal to
local size_per, health_in_pix = 200 / split_into, (200 / max_health) * health
for i = 0, split_into - 1, 1 do
local x, y = 3 + (size_per + 3) * i, H - 13 -- paranoia
-- background
draw.RoundedBox(0, x, y, size_per, 10, Color(40, 40, 40, 180))
-- translation: we basically check how much health each bar should have by taking the
-- total amount of health and removing the amount that the other bars would have
draw.RoundedBox(0, x, y, math.Clamp(health_in_pix - size_per * i, 0, size_per), 10, Color(150, 0, 0))
end
end
Got an error while doing this
Error:
[ERROR] lua/autorun/client/cl_hud_load.lua:106: ')' expected (to close '(' at line 105) near 'if'
1. unknown - lua/autorun/client/cl_hud_load.lua:0
Code:
aAR = Lerp(10 * FrameTime(), aAR, LocalPlayer():Armor())
local armor = LocalPlayer():Armor()
local bar1Perc = math.Clamp(armor, 0, 25) / 25
local bar2Perc = math.Clamp(armor - 25, 0, 75 / 75
if (bar1Perc > 0) then
draw.RoundedBox( 0, ScrW() * 0.373, ScrH() * 0.800, 4.6 * aAR, 15, Color( 255, 255, 255, 255))
if (bar2Perc > 0) then
draw.RoundedBox( 0, ScrW() * 0.373, ScrH() * 0.800, 4.6 * aAR, 15, Color( 255, 255, 255, 255))
end
end
Unless it's just me the code blocks on the forums are acting funky right now. You are indeed missing a closing parenthesis at this section:
local bar2Perc = math.Clamp(armor - 25, 0, 75 / 75 --[[ should be --]]
local bar2Perc = math.Clamp(armor - 25, 0, 75) / 75
Also, even with that syntax fixed your code won't give you what you want there. Instead, you should:
Set local armor to aAR
Shift bar 2's X position to where bar 1 would stop if it were full
Instead of 4.6 * aAR, use (for example) 115 * bar1Perc for the first bar, and 345 * bar2Perc for the second bar.
I now have this, but still has an error. Not sure where this is coming from.
Error (It's yellow in the console):
[ERROR] lua/autorun/client/cl_hud_load.lua:170: '<eof>' expected near 'end'
1. unknown - lua/autorun/client/cl_hud_load.lua:0
Code:
local armor = LocalPlayer():Armor()
local bar1Perc = math.Clamp(armor, 0, 25) / 25
local bar2Perc = math.Clamp(armor - 25, 0, 75) / 75
if (bar1Perc > 0) then
draw.RoundedBox( 0, ScrW() * 0.373, ScrH() * 0.800, 115 * bar1Perc, 15, Color( 255, 255, 255, 255))
if (bar2Perc > 0) then
draw.RoundedBox( 0, ScrW() * 0.373, ScrH() * 0.800, 345 * bar2Perc, 15, Color( 255, 255, 255, 255))
end
end
You have too many ends. I don’t know where, but it’s expecting the end of file <eof> after an end on line 170
That's fixed, but only got one problem left.
I tryed to make a third bar, bar3Perc, but it somehow acts strangly.
I tryed to fix it myself many times, but still ends up on the same problem. It keeps starting at 60 Armor?
My idea is so the first bar starts at 0 > 25, second bar 25 > 50, third bar 50 > 75 and fourth bar 75 > 100.
But my code doesn't seem to work?
aAR = Lerp(10 * FrameTime(), aAR, LocalPlayer():Armor())
local armor = LocalPlayer():Armor()
local bar1Perc = math.Clamp(aAR, 0, 25)
local bar2Perc = math.Clamp(aAR - 25, 0, 50)
local bar3Perc = math.Clamp(aAR - 50, 0, 75)
local bar4Perc = math.Clamp(aAR - 75, 0, 100)
-bump-
No idea why can't you just take what I gave you, but if you want it your way...
aAR = Lerp(10 * FrameTime(), aAR, LocalPlayer():Armor())
local bar_len_1 = math.Clamp(aAR, 0, 25)
local bar_len_2 = math.Clamp(aAR - 25, 0, 25)
local bar_len_3 = math.Clamp(aAR - 50, 0, 25)
local bar_len_4 = math.Clamp(aAR - 75, 0, 25)
Btw, numbers in variables is not a nice practice, in most cases there are much better alternatives.
Sorry, you need to Log In to post a reply to this thread.