HealthBar not updating after player reset

Hello there developers. I am trying to achieve a custom healthbar gui. The main problem is as the title says that it does not update after a player dies or resets. I have currently tried to rescript the healthbar twice with the same result. By documenting myself I understood that this issue might be created by the fact that once a player resets or dies they get a new character/humanoid.

Code:

local function updateHealthBar()
	local health = humanoid.Health / humanoid.MaxHealth
	local tweenInfo = TweenInfo.new (0.5)
	local tween = TS:Create(
		healthbar,
		tweenInfo,
		{Size = UDim2.fromScale(health, 1)}
	)
	tween:Play()
end

updateHealthBar()
humanoid:GetPropertyChangedSignal('Health'):Connect(updateHealthBar)

You’re right it may be an issue that the health isn’t “changed” and firing the event when a new character arrives. Perhaps try using Player.CharacterAdded to detect they’ve respawned, and update their health accordingly?

I have tried adding this block of code but it still doesn’t work:

game.Players.ChildAdded:Connect(function(char)
	character = char
	updateHealthBar()
end)

Does the gui have ResetOnSpawn enabled? That might solve the issue.

Should that option be disabled?

That option should be enabled.
(Sorry for not specifying)

Could you show an image of where the script is located?

I’m afraid it is enabled and I have noticed that instead of player.CharacterAdded I typed Players.ChildAdded but that still didn’t solve the issue.

Script Location

Remove the PlayerAdded function, it should be working properly. If ResetOnSpawn is enabled on the gui and you reset the player, it’s just like as if you just joined the game.

I have actually moved my script from StarterPlayerScripts to StarterGui and that fixed the issue, thank you very much.

2 Likes

yea that explains it,
put it in starter gui