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

Why won't my health display work?

for i, player in pairs(game.Players:GetChildren()) do
	if player:FindFirstChild("PlayerGui") then
		local plr = player
		for i, object in pairs(game.Workspace:GetChildren()) do
			if object:FindFirstChild("Humanoid") then
				local humanoid = object:FindFirstChild("Humanoid")
				if humanoid.Parent.Name == plr.Name then
					local healthStatus = plr.PlayerGui.healthGui.healthFrame.healthStatus
					healthStatus.Text = humanoid.Health.." / "..humanoid.MaxHealth
				end
			end
		end
	end
end

So after lots of different attempts with different approaches, I really thought this one would work but (clearly) it did not, but I can't figure out why?

(Goal of this script is to change the text of healthStatus to whatever the players health is out of their max health)

3 comments

0

You're code is only running once, unless there is code you haven't included.

0

Is it a script or a localscript?

0

It was just a normal script

1 answer

✓ Accepted answer

This should work if you intend this to be on the server, given you don't have FE (FilteringEnabled) on. You didn't connect your code to run whenever a new player was added or when the health changed. You're script only ran once as soon as it loaded and never executed the code again.

function connectChanges(plr)
	plr.CharacterAdded:connect(function()
		local humanoid = plr.Character:FindFirstChild("Humanoid")
		if humanoid then
			
			local healthStatus = plr.PlayerGui:FindFirstChild("healthStatus", true) --true makes it recursive. Check the wiki for more
			humanoid.changed:connect(function()
				healthStatus.Text = humanoid.Health .. " / " .. humanoid.MaxHealth
			end)
		end
	end)
end

for i, plr in pairs(game.Players:GetChildren()) do
	connectChanges(plr)
end

game.Players.PlayerAdded:connect(function(plr)
	plr.CharacterAdded:connect(function()
		connectChanges(plr)
	end)
end)

If you intended it to be on the client as a localscript (probably the better option) You need a variation on the last bit of code.

local plr = game.Players.LocalPlayer

plr.CharacterAdded:connect(function()
	local humanoid = plr.Character:WaitForChild("Humanoid")
	local healthStatus = plr.PlayerGui:FindFirstChild("healthStatus", true) --true makes it recursive. Check the wiki for more
	humanoid.changed:connect(function() --this is the vital part missing. Without it, the gui will never update.
		healthStatus.Text = humanoid.Health .. " / " .. humanoid.MaxHealth
	end)
end)

As you can see, I took out the parts connect the function in the event of players being added because local scripts will reload and execute whenever the character spawns. (If I'm wrong, use the other code without the loop going through all players)

4 comments

0

Thank you so much :D

1

I didn't actually test it, so tell me if it works.

0

Yep, it does! One last question tho, is there a way to make it so it doesn't show all the decimal points while regenerating health? (or just if you can turn off health regen in general)

1

On line 7 of my second script use math.floor(humanoid.Health) to truncate (get rid of) the decimal.

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