Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why when my health bar gets to 0, it get's weird and gets outside the frame. How do I fix it?

https://gyazo.com/4cbf374bf42c8990918fb3010430ca5e

It's just a simple healthbar script. Btw I'm still new to scripting.

local player = game.Players.LocalPlayer
local health = script.Parent
local character = player.Character or player.Character:Wait()
local hum = character:WaitForChild("Humanoid")

	while wait() do
		
		local percent = hum.Health / hum.MaxHealth
	health.Size = UDim2.fromScale(percent - 0.06, 0.574) --hmmmmm
	health.Text = math.floor(player.Character.Humanoid.Health)
	end

1 answer

✓ Accepted answer

Maybe use math.clamp() when setting percent's value? If the health is less than 0 then you will get a negative number. math.clamp(percent, 0, 1) would keep the value between 0 and 1. To clarify, your new script would look like this:

local player = game.Players.LocalPlayer
local health = script.Parent
local character = player.Character or player.Character:Wait()
local hum = character:WaitForChild("Humanoid")

while wait() do
    local percent = hum.Health / hum.MaxHealth
    percent = math.clamp(percent, 0, 1)
    health.Size = UDim2.fromScale(percent - 0.06, 0.574) --hmmmmm
    health.Text = math.floor(player.Character.Humanoid.Health)
end

I'm not too familiar with using math.clamp but I am pretty sure that this will fix your problem. Let me know if it doesn't.

1 comment

0

Thank you so much, it worked! I just changed the math.clamp(percent, 0.05, 1).

This discussion is preserved as it was. New answers, comments, and votes are closed.