Break out of loop

I loop through the children of the player, and if a child is found, the remote event should NOT fire and it should continue to whatever code is after the loop and the initial if statement. How do I do this? My break statements are not working, my debug print does print correctly, but the remote event still fires. Here is the code:

if plr:WaitForChild("Paired").Value == "" then
		for _, child in ipairs(plr:GetChildren()) do
			if child.Name == "Walker" and child.Value == tostring(plrWT) then
				print('this')
				break
			end
		end
		game.ReplicatedStorage.SendInfo:FireClient(plrWT, plr)
	end

Please help, thanks.

The remote event fires because it’s outside of the loop, all the “break” does it breaks out of the loop, to fix this put the remote event into an else
so it would look like this:

if plr:WaitForChild("Paired").Value == "" then
		for _, child in ipairs(plr:GetChildren()) do
			if child.Name == "Walker" and child.Value == tostring(plrWT) then
				print('this')
				break
            else
                game.ReplicatedStorage.SendInfo:FireClient(plrWT, plr)
			end
		end
	end

sorry for the weird indentation, I wrote it in the DevForum not studio

This wouldn’t work as the if statement would iterate through for every child. For example, if the last item iterated through is not a string value, then it would break out before reaching it. I tried this as well.

You can use a bool variable to check whether the child is found. It would look something like this:

local ChildFound = false

for loop do
     if condition met then
          ChildFound = true
          break
     end
end

if ChildFound == false then
     -- fire remote here
end 
1 Like

It works, thanks for the input. I appreciate it.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.