Health bar not working as expected

  1. What do you want to achieve?
    I’m trying to make a health bar.

  2. What is the issue?
    The problem is that when the player’s health go down and it’s not a number under 43 it wont do anything, however if the health goes lower than 43 it will work. I tried changing the size of my bar and it just made that the bar showed effects if the health is under a lower number like 60, I don’t want to keep doing that because the bar will cover the whole screen.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    None of the solutions I’ve tried have actually worked, I tried looking in Devforum to see if anybody else had the same problem but apparently no one.

Humanoid.HealthChanged:Connect(function(dmg)
	local Health = math.clamp((dmg / Humanoid.MaxHealth),0,0.417)
	Healthbar:TweenSize(UDim2.fromScale(Health, Healthbar.Size.Y.Scale), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.5, false, nil)
	wait(0.5)
	HealthRedMeter:TweenSize(UDim2.fromScale(Health, HealthRedMeter.Size.Y.Scale),Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.5, false, nil)
end)

The issue is that the ratio dmg / Humanoid.MaxHealth has a range 1 to 0 where if it is equal to 1 it will be at full health and 0 at no health.

And this ratio is being clamped to 0.417 so 100 maxHealth*0.417 = 41.7 so it will remain stuck at health 41.7, as described here:

The solution is to only clamp 0,1 from no health to full health then only after that you apply a multiplier in order to scale down the ratio in order to avoid it covering the full screen so something like this:

Humanoid.HealthChanged:Connect(function(currenHealth)
	local Health = math.clamp((currenHealth/ Humanoid.MaxHealth),0,1)
Health *= 0.75 --multiplier to avoid it covering the screen.
	Healthbar:TweenSize(UDim2.fromScale(Health, Healthbar.Size.Y.Scale), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.5, false, nil)
	wait(0.5)
	HealthRedMeter:TweenSize(UDim2.fromScale(Health, HealthRedMeter.Size.Y.Scale),Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.5, false, nil)
end)

1 Like