Health Display script only running once

I wrote this script yesterday at night and i was pretty excited, but for some reason this script only seems to run once, it works and after the player dies it doesn’t work anymore, any help?

Here’s the breakdown:

1
The Name storage stores the player’s name when it enters the workspace.
This is assuming that the GUI will be on the Players section, where it usually goes, inside of the player.

The name storage script:

2
(I haven’t had issues with this script, its very simple but works well for me, posting it in here just in case someone finds an issue with it)

The health script that i am currently having issues on:

Here is the script functioning correctly, displaying health on the first use

And here it is after dying

It displays the placeholder text and the health just doesn’t update anymore, so far i’ve tried different methods of running the script, instead of childadded, such as detecting when the player respawns but up to no avail, script output doesn’t display any messages, but occassionally says that “Humanoid” is not a part of Workspace.DatPisidukM (aka. the player), any ideas please let me know and i’ll put them to the test right away, thanks in advace :slight_smile:

Here’s the code

local workspacef = game:GetService("Workspace")

workspacef.ChildAdded:Connect(function()
	local found = workspacef:FindFirstChild(script.Parent.Parent.Parent.Parent.NameStorage.Value)
	if (found ~= nil) then
		print("humanoid found")
		print(found.Humanoid.Health)
		script.Parent.Text = found.Humanoid.Health
	else
		error("idk didn't work")
	end
	
	found.Humanoid.HealthChanged:Connect(function()
		script.Parent.Text = found.Humanoid.Health
		if found.Humanoid.Health > 60 then
			script.Parent.TextColor3 = Color3.new(0.0666667, 1, 0)
			
		elseif found.Humanoid.Health < 60 then
			script.Parent.TextColor3 = Color3.new(1, 0.694118, 0.388235)
		end
		
	end)
	
	found.Humanoid.Died:Connect(function()
		script.Parent.Parent.Parent.Parent:Remove()
	end)
end)
1 Like

Your character is probably spawning in before the UI is loading the script.
That or the value is not being changed quick enough upon loading up.

Aside from all of that, you can get the LocalPlayer using the Players Service like this:

local Player = game.Players.LocalPlayer

You can also get the player’s character and name from it too.

local Character = player.Character
local PlayerName = player.Name

With this information you could just have it wait for the Player Character’s humanoid and then set the text to the health of the player, the same way you did before.

Character.Humanoid.HealthChanged:Connect(function()
		script.Parent.Text = Character.Humanoid.Health
		if Character.Humanoid.Health > 60 then
			script.Parent.TextColor3 = Color3.new(0.0666667, 1, 0)
		elseif Character.Humanoid.Health < 60 then
			script.Parent.TextColor3 = Color3.new(1, 0.694118, 0.388235)
		end
end)
2 Likes

Try this in StarterCharacterScripts. Scripts inside here will get cloned to the character each time they spawn so all you have to do is use script.Parent to get the current character and you won’t have to worry about using your NameStorage value and the other local script you had. Let me know if you have any issues.

local Player = game.Players.LocalPlayer
local PlayerGui = Player.PlayerGui
local HealthDisplay = PlayerGui:WaitForChild("Tobechanged").Frame.TextLabel.HealthDisplay

local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")

Humanoid.HealthChanged:Connect(function()
	HealthDisplay.Text = Humanoid.Health
	if Humanoid.Health >= 60 then
		HealthDisplay.TextColor3 = Color3.new(0.0666667, 1, 0)
	elseif Humanoid.Health < 60 then
		HealthDisplay.TextColor3 = Color3.new(1, 0.694118, 0.388235)
	end
end)
2 Likes

YOOO it worked TYSM :D, it was finally working and it was way simpler than i tought! TY

2 Likes

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