Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Health GUI doesn't show players health?

I created a GUI that is supposed to show the player's health, but it doesn't. Here is the code.

game:GetService("Players").PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(char)
while 1+1==2 do
	script.Parent.Text = char:WaitForChild("Humanoid").Health
end
end)

2 answers

✓ Accepted answer

doing a loop like that without a wait can crash your game

and you forgot to put another end) in the end of your script

with fixes:

game:GetService("Players").PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(char)
while true do wait(0.35)
    script.Parent.Text = char:WaitForChild("Humanoid").Health
end
end)
end)

also using a loop for every player that comes is not very good and can still lag the server but i dont think the server can detect when a player's humanoid is changed so in a localscript you can instead do:

local hum = game:GetService("Players").LocalPlayer.Character:WaitForChild("Humanoid")

hum.Changed:Connect(function(property)
	if property == "Health_XML" or property == "Health" then
		script.Parent.Text = hum.Health
	end
end)

Try this?

game:GetService("Players").PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(char)
while true do
    local humanoid = char:WaitForChild("Humanoid")
    local health = humanoid.Health
    script.Parent.Text = health
end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.