Hello!
I’m trying to make a radial health bar for the player using UIGradient and with the help of this forum post.
So I designed a radial health bar around the player’s feet to indicate the player’s health.
I used :GetPropertyChangedSignal() to figure out if the player’s health is being changed, and then I hooked it up with this code below:
Code
--Services
local Players = game:GetService('Players')
--Player
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild('Humanoid')
--Variables
local Main = script.Parent.Main
local Health, MaxHealth = Humanoid.Health, Humanoid.MaxHealth
--Functions
local function CurvedBarResize(bar, alpha)
print(alpha)
bar.UIGradient.Rotation = -90 * (1 - alpha)
if alpha - 0.001 <= 0 then
bar.UIGradient.Transparency = NumberSequence.new(1, 1)
elseif alpha == 1 then
bar.UIGradient.Transparency = NumberSequence.new(0, 0)
else
local subtract = alpha - 1
bar.UIGradient.Transparency = NumberSequence.new({
NumberSequenceKeypoint.new(0, 0),
NumberSequenceKeypoint.new(subtract, 0),
NumberSequenceKeypoint.new(alpha, 1),
NumberSequenceKeypoint.new(1, 1)
})
end
end
local function UpdateHud()
local circleAlpha = MaxHealth / 3
local bottomRightAlpha = MaxHealth * (2/3) --66
local topRightAlpha = MaxHealth * (2/4.5) -- 44
local topLeftAlpha = MaxHealth * (1/3) --33
local bottomLeftAlpha = MaxHealth * (1/8) --12.5
if Health <= MaxHealth then
if Health >= bottomRightAlpha then
Main.BottomRight.Green.UIGradient.Transparency = NumberSequence.new(0, 0)
Main.TopRight.Green.UIGradient.Transparency = NumberSequence.new(0, 0)
Main.BottomLeft.Green.UIGradient.Transparency = NumberSequence.new(0, 0)
Main.TopLeft.Green.UIGradient.Transparency = NumberSequence.new(0, 0)
local alpha = (Health - bottomRightAlpha) / circleAlpha
CurvedBarResize(Main.BottomRight.Green, alpha)
elseif Health >= topRightAlpha then
Main.BottomRight.Green.UIGradient.Transparency = NumberSequence.new(1, 1)
Main.TopRight.Green.UIGradient.Transparency = NumberSequence.new(0, 0)
Main.BottomLeft.Green.UIGradient.Transparency = NumberSequence.new(0, 0)
Main.TopLeft.Green.UIGradient.Transparency = NumberSequence.new(0, 0)
local alpha = (Health - topRightAlpha) / circleAlpha
CurvedBarResize(Main.TopRight.Green, alpha)
elseif Health >= topLeftAlpha then
Main.BottomRight.Green.UIGradient.Transparency = NumberSequence.new(1, 1)
Main.TopRight.Green.UIGradient.Transparency = NumberSequence.new(1, 1)
Main.BottomLeft.Green.UIGradient.Transparency = NumberSequence.new(0, 0)
Main.TopLeft.Green.UIGradient.Transparency = NumberSequence.new(0, 0)
local alpha = (Health - topLeftAlpha) / circleAlpha
CurvedBarResize(Main.TopLeft.Green, alpha)
elseif Health >= bottomLeftAlpha then
Main.BottomRight.Green.UIGradient.Transparency = NumberSequence.new(1, 1)
Main.TopRight.Green.UIGradient.Transparency = NumberSequence.new(1, 1)
Main.BottomLeft.GreenUIGradient.Transparency = NumberSequence.new(0,0)
Main.TopLeft.Green.UIGradient.Transparency = NumberSequence.new(1,1)
local alpha = (Health - bottomLeftAlpha) / circleAlpha
CurvedBarResize(Main.BottomLeft.Green, alpha)
end
end
end
Humanoid:GetPropertyChangedSignal('Health'):Connect(UpdateHud)
However, there are some problems with this.
Whenever I try to change the player’s health this happens:
If I set the player’s health to 50, for example, the health bar will still display the same thing as the image above.
Is there a way I could make it work properly? Thanks in advance.