I am trying to make a health bar that uses hearts. I’m trying to use 3 frames and clip decedents to give the illusion of separate images. This diagram is what I’m trying to achieve:
(These images overlap on each other, separated for visual example, highlighted grey is what will be visible)
I am having difficulty getting the positioning math correct- For my use I only need the maxhealth to be scalable to 10, but I imagine if done correctly you can use any amount of health
local MaxHealth = Humanoid.MaxHealth
local Health = Humanoid.Health
if MaxHealth%2== 0 then
script.Parent.Health.Position = UDim2.new(-1+(MaxHealth)/10,0,0,0)
script.Parent.Health.HealthClip.Position = UDim2.new(-1+Health/10,0,0,0)
script.Parent.Health.HealthClip.HealthShow.Position = UDim2.new(-1+MaxHealth/10,0,0,0)
else
--Odd numbers positiong is different to leave half a heart
script.Parent.Health.Position = UDim2.new(-0.9+(MaxHealth)/10,0,0,0)
script.Parent.Health.HealthClip.Position = UDim2.new(-0.9+Health/10,0,0,0)
script.Parent.Health.HealthClip.HealthShow.Position = UDim2.new(0.9+MaxHealth/10,0,0,0)
end
I don’t think you need two cases here. Seems to me that all you really need to do is divide current health by max health, which would be on a scale of 0 to 1.
local MaxHealth = 10
local Health = 9
print(Health / MaxHealth) -- 0.9
Since you need this frame to move in a negative direction you just add this number to -1 like you’re doing in your code currently.
local MaxHealth = 10
local Health = 9
print(-1 + (Health / MaxHealth)) -- -0.1
Assuming that the hearts are positioned correctly within your frame then this should just work. Your code ends up being
local MaxHealth = Humanoid.MaxHealth
local Health = Humanoid.Health
local Offset = -1 + (Heath / MaxHealth)
script.Parent.Health.Position = UDim2.fromScale(Offset, 0)
script.Parent.Health.HealthClip.Position = UDim2.fromScale(Offset, 0)
script.Parent.Health.HealthClip.HealthShow.Position = UDim2.fromScale(Offset, 0)
If your hearts aren’t positioned correctly then let me know and we can go from there.