For Loop Getting Stuck

Hello!, I’m having trouble using this script I made, the while loop will run 2 times and the for loop 2 times, it gets stuck before starting the while loop again as you can see in the output;
output

Here is the script, any help would be appreciated.

while true do
	wait()
	print("while")
	for i, v in pairs(game.Players:GetPlayers()) do
		print("for")
		local character = v.CharacterAdded:wait()
		character.ChildAdded:Connect(function(child)
			if child.Name == "Owner" then
				print("child added")
				listenerevent:FireAllClients()
			end
		end)
		
	end
	PlayersService.PlayerRemoving:Connect(function(funado)
		
		print("funado")
		listenerevent:FireAllClients()
	
	end)
end

1 Like

Why are you using a for loop inside of a while loop?

edit : you might be causing a memory leak @LordKemono

1 Like

never use connections inside of an infinite loop

1 Like

my guess is that the server is crashing because of all the connections you have lol

I want it to be infinite, any ideas?

Why do you have to “wait()” cant you just use wait(1)?

edit : the loop is running continuously and at a very fast rate thats not good

you don’t need 30 connections that do the same thing

2 Likes

I need the childadded event to listen to all player’s characters. And to not end.

1 Like

use this instead

game.Players.PlayerAdded:Connect(function(player)
  player.CharacterAdded:Connect(function(character)
    -- do stuff with character
   end)
end)
1 Like

Yeah I know, by making this in an infinite loop I will definitely have problems, but I couldn’t find another way.

Yeah just use a player added event, everytime the player dies it refires

The thing is I’m making a tycoon, and when the player touches the owner door, the player gets a owner “tag” that is just a value inside the player character, I need to listen to when this value is added.

1 Like
game.Players.PlayerAdded:Connect(function(player)
  player.CharacterAdded:Connect(function(character)
     z = character.ChildAdded:Connect(function(child)
           if child.Name == "tag or whatever" then
             -- do whatever
             z:Disconnect()
           end
         end)
   end)
end)
1 Like

Wouldnt it be when it gets changed not added?

1 Like

Thanks!!! That worked, I just changed the child.Name because it didn’t recognize it and it worked, Thanks a lot! @Sniperkaos and @IProgramForFun :smiley:

1 Like