Health Bar UI not working when respawning

when the user resets the health bar doesnt work anymore. (I am talking about the ui one. So the moment the user resets, the bar stops going down how can I fix that issue

-->> HealthBar Animations <<--
local TweenService = game:GetService("TweenService")

local player = game.Players.LocalPlayer
local playergui = player.PlayerGui
local ScreenGui = playergui:WaitForChild("ScreenGui")
local HealthBarFrame = ScreenGui:WaitForChild("HealthBarFrame")
local HealthGreen = HealthBarFrame:WaitForChild("HealthGreen")
local HealthTxt = HealthBarFrame:WaitForChild("HealthTxt")

local originalSize = HealthGreen.Size

local function updateHealth(humanoid)
	local currentHealth = humanoid.Health
	local maxHealth = humanoid.MaxHealth
	local healthPercentage = currentHealth / maxHealth

	local newSize = UDim2.new(healthPercentage * originalSize.X.Scale, 0, originalSize.Y.Scale, 0)
	local tweenInfo = TweenInfo.new(0.4, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out)
	local healthTween = TweenService:Create(HealthGreen, tweenInfo, {Size = newSize})

	healthTween:Play()

	HealthTxt.Text = string.format("%d / %d", currentHealth, maxHealth)
end

local function connectHealthChange(character)
	local humanoid = character:WaitForChild("Humanoid")
	humanoid.HealthChanged:Connect(function()
		updateHealth(humanoid)
	end)
end

player.CharacterAdded:Connect(connectHealthChange)
if player.Character and player.Character:FindFirstChild("Humanoid") then
	connectHealthChange(player.Character)
end

Video example : Watch 2024-05-07 19-20-35 | Streamable

Is this script located in StarterCharacterScripts, StarterPlayerScripts or an UI element?

1 Like

Thanks, you answered my question. It was in player, I put it in character. TY