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)
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)