Yet another question I couldn't find the answer to

I’m working with bars and such in LUA. At the moment I have a health bar that is fixed width to 15% of the screen width using the code…

local healthBarWidth = ScrW() * 0.15

The actual health of the player is on top of that and is using the code…

local healthBarPlayerHealthWidth = playerHealth * (healthBarWidth / 100)

playerHealth is defined as LocalPlayer():Health()

The only problem with using this method is that when the player has over 100 health it goes off the background bar. I’m trying to find a way of converting the players health into a percentage of 100. So that no matter what the players health is it will always fit into any size.

I’m using an IF block at the moment to check if the players health is over 100 and just limiting the size of the bar to nothing over the width of the fixed background bar.

It would be most appreciated if someone could like me to a tutorial or reference to doing this.

I wouldn’t mind so much but I am going to add some form of experience bar later on that will have all kinds of possible values and it’s going to make my life very complicated when I come to that bridge.

Thanks in advance.

(User was banned for this post ("Undescriptive thread title" - mahalis))

playerHealth = math.Clamp(playerHealth,0,100)

math.Clamp allows you to clamp a value between a min and max.

local healthBarWidth = ScrW() * 0.15
local currentWidth = pl:Health() / pl:GetMaxHealth() * healthBarWidth [/lua]

The only problem is that GetMaxHealth() returns 1 on the client; I solved that issue by networking it manually.

what about:

if playerHealth > 100 then
playerHealth = 100


That’s what math.Clamp does.

[quote=“iRzilla, post:5, topic:75783”]

Need to end the quote tag.

[quote=“supersnail11, post:6, topic:75783”]

Thank’s so much! <3 You’re awesome!

I’m not quite sure how math.Clamp works. But wouldn’t that only lower the health bar when it reached below 100? So if it was 150. Between 100 and 150 it would remain at full width and not get shorter?

– local a = math.Clamp(value, 100, 200)
– or a = aClamp(value, 100, 200)

function aClamp(value, min, max)
if value < min then
return min
elseif value > max then
return max

return value

Why did I not think of that. Thanks a lot dude.

I didn’t read this post, this exactly what I wanted, thanks a lot dude.

ps. This is Bradster, account got banned for 12 hours for posting an undescriptive topic title. A little extreme but heh.