How do i prevent my healthbar from going off the screen?

Im creating a healthbar for my game, heres the script:

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Health, false)

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local healthbar = script.Parent.Frame:WaitForChild("HealthBar")

game:GetService("RunService").RenderStepped:Connect(function()
	local humanoid = char:FindFirstChild("Humanoid")
	healthbar.Health.Size = UDim2.new(humanoid.Health/humanoid.MaxHealth,0,1,0)
end)

When you load in everything is fine but when the player gains more health the gui goes off the screen and the only way i found to fix this is by resetting. When you reset the gui works perfectly.

So what do i have to do to make the gui work as the player gains strength without having to reset

1 Like

I’m wondering why you’re using RenderStepped & not the Humanoid.HealthChanged event :thinking:

Could you try this?

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Health, false)

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local healthbar = script.Parent.Frame:WaitForChild("HealthBar")
local humanoid = char:WaitForChild("Humanoid")

humanoid.HealthChanged:Connect(function()
    healthbar.Health.Size = UDim2.new(humanoid.Health / humanoid.MaxHealth, 0, 1, 0)
end)

i’ve tried that aswell and tested your script but it seems to be doing the same thing


This is what happens when you gain more health

You seem to be increasing the Health value but not the MaxHealth value?

Is your health bar text right…? Cause it looks like it’s doing the opposite

Ahhh now i see the problem, the player started off with more health than max health so when you gained more max health the gui couldn’t keep up. As always i appreciate your help (: