Loops inside loops

im making a nametag system that displays players hp on them as a gui. its activated by a gui button so its local for each player. so when a player clicks on button. he sees everyones name and hp above their head. but others dont see that until they click the button.

to do that i use

local Players = game.Players:GetChildren()
for i,v in pairs(Players) do
 -- a gui here shows their hp above their head.
end

i want gui to update every second so when they heal and regen.
gui will show their updated hp.
i tried to put while wait() do OR repeat until bla bla to do that
but when i put it inside for i,v in pairs()
it displays gui for just one player and no others.
how can i put a loop inside for i,v in pairs

You could try putting your while loop in a spawn() function

If your doing this in a local script then thats why it will only display for one user because the way that local scripts work is they work locally meaning only the local player will see it.

Also dunno if this would change anything but in the for i, v in pairs you might wanna do the game.Players:GetChildren() as the thing to loop through as I think that might affect it

Also to detect when the player heath changes u probs could use this if not already. Humanoid.HealthChanged

“Also dunno if this would change anything but in the for i, v in pairs you might wanna do the game.Players:GetChildren() as the thing to loop through as I think that might affect it”
this type fixed my problem. thanks

Use players:GetPlayers() instead, in case non-player instances are parented to the players service.

2 Likes