Heart Healthbar Remove last heart way too early

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()

One way to get around that is to use math.ceil instead of math.floor, divide by 25 instead of 20, then add 1 if the health is equal to 100.

i.e.

local FullHeart = math.ceil(Humanoid.Health / 25) + (Humanoid.Health == 100 and 1 or 0)
Outcome


1 Like

This could work normally but the problem is I have another script that handle player revive function and that script prevent player health from going below 0

image

which mean the last heart will always showing no matter what now
is there a way to fix that?

ok this work

local FullHeart = math.ceil(Humanoid.Health / 25) + (Humanoid.Health == 100 and 1 or 0) - (Humanoid.Health <= 1 and 1 or 0)