Gui not updating after respawn

So basically I have a health GUI that keeps track of Humanoid Health. Here is the GUI when I’m alive:

image

And this is when I respawn:
image

The script I am using:

image

How do I make it so that when I respawn the bar updates?

Try this (im on mobile)

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hbararea = script.Parent.Parent

local hpbar = hbararea.Green

local hp = char.Humanoid.Health
local maxhp = char.Humanoid.MaxHealth
hpbar.Parent.Health.Text = hp … “/” … maxhp
hpbar.Size = UDim2.new(hp / maxhp, 0, 1, 0)

plr.CharacterAdded:Connect(function(newChar)
char = newChar

char:WaitForChild("Humanoid").HealthChanged:Connect(function()
    hp = char.Humanoid.Health
    maxhp = char.Humanoid.MaxHealth
    hpbar.Parent.Health.Text = hp .. "/" .. maxhp
    hpbar.Size = UDim2.new(hp / maxhp, 0, 1, 0)
end)

hp = char.Humanoid.Health
maxhp = char.Humanoid.MaxHealth
hpbar.Parent.Health.Text = hp .. "/" .. maxhp
hpbar.Size = UDim2.new(hp / maxhp, 0, 1, 0)

end)

while task.wait(0.1) do
if char and char:FindFirstChild(“Humanoid”) then
hp = char.Humanoid.Health
maxhp = char.Humanoid.MaxHealth
hpbar.Parent.Health.Text = hp … “/” … maxhp
hpbar.Size = UDim2.new(hp / maxhp, 0, 1, 0)
end
end

Check this out:

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

local PlayerGui = LocalPlayer:WaitForChild("PlayerGui")
local ScreenGui = PlayerGui:WaitForChild("ScreenGui")
local Frame = ScreenGui:WaitForChild("Frame")
local LoadingBar = Frame:WaitForChild("LoadingBar")

local function DisplayHealth(health, maxHealth)
	local Width = math.clamp((health / maxHealth) * Frame.Size.X.Offset, 0, Frame.Size.X.Offset)
	LoadingBar.Size = UDim2.new(0, Width, LoadingBar.Size.Y.Scale, LoadingBar.Size.Y.Offset)
end

local function HealthConnection()
	local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
	local Humanoid = Character:WaitForChild("Humanoid")

	DisplayHealth(Humanoid.Health, Humanoid.MaxHealth)

	Humanoid.HealthChanged:Connect(function(health)
		DisplayHealth(health, Humanoid.MaxHealth)
	end)
end

LocalPlayer.CharacterAdded:Connect(HealthConnection)

if LocalPlayer.Character then
	HealthConnection()
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.