I was making a Heart bar GUI that display The amout of heart relatively to how much hp you currently have, each heart is equal to 20
there is a problem however
as you can see in the video, if you have less than 20 health left your last heart will be gone even tho you didn’t actually die just yet
script
local player = game:GetService("Players").LocalPlayer
local Char = player.Character or player.CharacterAdded:Wait()
local Humanoid = Char:WaitForChild("Humanoid")
local HeartIcon = game:GetService("ReplicatedStorage").Assets.Heart
local connection_health
local connection_maxhealth
local function update()
for _, OldHeart in pairs(script.Parent:GetChildren()) do -- destroy old heart
if OldHeart:IsA("ImageLabel") then
OldHeart:Destroy()
end
end
local FullHeart = math.floor(Humanoid.Health/20)
if Humanoid.Health > 0 then -- create new heart
for i = 1,FullHeart, 1 do
local NewHeart = HeartIcon:Clone()
NewHeart.Name = "NewHeart"
NewHeart.Parent = script.Parent
end
end
end
local function connections()
connection_health = Humanoid:GetPropertyChangedSignal("Health"):Connect(update)
connection_maxhealth = Humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(update)
update()
end
player.CharacterAdded:Connect(function(new)
if connection_health then connection_health:Disconnect() end
if connection_maxhealth then connection_maxhealth:Disconnect() end
Char = new
Humanoid = Char:WaitForChild("Humanoid")
connections()
end)
connections()