Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago

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

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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.

0
Thank you so much, it worked! I just changed the math.clamp(percent, 0.05, 1). RichDiggerW189 2 — 3y
Ad

Answer this question