Help with Health Bar

Hello!

I made a simple Health Bar, but every time I reset, it is stuck at zero and never goes back up to 100.

Screen Shot 2022-07-23 at 4.29.54 PM

Here is the code I have for it:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

humanoid.HealthChanged:Connect(function(Damage)
	script.Parent:WaitForChild("HealthBar").GreenFrame.Size = UDim2.new(Damage / humanoid.MaxHealth, 0, 1, 0)
	script.Parent:WaitForChild("HealthBar").HealthStatus.Text = ((Damage / humanoid.MaxHealth) * 100).."%"
end)

Help please!

Thanks

-GreenTree Gaming

Try setting ResetOnSpawn property under the ScreenGui to true.

You should probably use this instead!

local HealthBar = script.Parent:WaitForChild("HealthBar")
local bar = HealthBar:WaitForChild("GreenFrame")
local hpText = HealthBar:WaitForChild("HealthStatus")
local player = game.Players.LocalPlayer
repeat wait() until player.Character
local connection_health
local connection_max_health
local character = player.Character

local function update()
	local humanoid = character:WaitForChild("Humanoid")
	bar:TweenSize(UDim2.new(humanoid.Health / humanoid.MaxHealth, 0, 1, 0), Enum.EasingDirection.In, Enum.EasingStyle.Quint, .1, true)
	hpText.Text = math.ceil(humanoid.Health)
end

local function setConnections()
	local humanoid = character:WaitForChild("Humanoid")
	connection_health = humanoid:GetPropertyChangedSignal("Health"):Connect(update)
	connection_max_health = humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(update)
	update()
end

player.CharacterAdded:Connect(function(char)
	character = char
	if connection_health then connection_health:Disconnect() end
	if connection_max_health then connection_max_health:Disconnect() end
	setConnections()
end)

setConnections()
1 Like

I think it is getting stuck cause’ you are not updating the character after the hum dies.

Try disabling ResetOnSpawn property on the ScreenGui and add this to ur script

humanoid.Died:Connect(function()

       local character = player.Character or player.CharacterAdded:Wait()
       local humanoid = character:WaitForChild("Humanoid")

end)

and if that doesn’t work then try

while true do

       local character = player.Character or player.CharacterAdded:Wait()
       local humanoid = character:WaitForChild("Humanoid")

       wait(.01)

end

and if that doesn’t work either try just maybe setting ResetOnSpawn property to true on the ScreenGui

or maybe ur entire original script is just not working