How do you break a loop using a statement that is not inside of the loop?

How do you break a loop using a statement that isn’t in the loop? I tried using break but I couldn’t make the loop that I want to break break. I also want to break another loop but I will just copy the one from the other. Here is my attempt on my script:

	local function ingame()
		for i = ingametime,0,-1 do
			wait(1)
			text.Value = ingametimetext..i
		end
		if text.Value == ingametimetext..stop then
			wait(1)
			for i, v in pairs(game.Players:GetPlayers()) do
				local char = v.Character
				local torso = char:FindFirstChild("Torso") or char:FindFirstChild("HumanoidRootPart")
				torso.CFrame = CFrame.new(game.Workspace.TelePart.Position)
			end

			text.Value = "Time's up!"
			wait(1)
			deb = false
			
		elseif #game.ReplicatedStorage.PlayerInGame:GetChildren() == stop then
			wait(1)
			for i, v in pairs(game.Players:GetPlayers()) do
				local char = v.Character
				local torso = char:FindFirstChild("Torso") or char:FindFirstChild("HumanoidRootPart")
				torso.CFrame = CFrame.new(game.Workspace.TelePart.Position)
			end
			text.Value = "Round is over!"
			wait(1)
			deb = false
		end
	end
	

breaking loops are helping countless people in the community. But this might be the most important question.

1 Like

maybe using return, it would stop everything if fore example, the torso.CFrame it’s on the CFrame

1 Like

Yea but I only want it to stop the loop that I want to break.

Here’s a thread that could probably help you with your inquiry. How can I break this loop? - #2 by inHeadspace

In your for loops, there are no breaks. It will run through all the pairs.

From what I can gather, you want those If statements to break that first for loop. You will need to have the If statements inside the for loop you want to break. Then those if statements have break to break the for loop that it’s are in.

You can make the loop when it triggers a break condition (like finding it’s target or something) then it has “break”

Now if it’s meant to break some other loop, it can change a variable “BreakLoops.Value” to true. Then all the loops and check the BreakLoops.Value and if it’s true, then the loops break. Then have it wait for all the other loops to break before setting the Value to False.

    local function ingame()
		for i = ingametime,0,-1 do
			wait(1)
			text.Value = ingametimetext..i	
			--breaking this loop
			if text.Value == ingametimetext..stop then
				wait(1)
				for i, v in pairs(game.Players:GetPlayers()) do
					local char = v.Character
					local torso = char:FindFirstChild("Torso") or char:FindFirstChild("HumanoidRootPart")
					torso.CFrame = CFrame.new(game.Workspace.TelePart.Position)
					--not this loop
				end	

				text.Value = "Time's up!"
				wait(1)
				deb = false
				break -- this will break the first loop
			elseif #game.ReplicatedStorage.PlayerInGame:GetChildren() == stop then
				wait(1)
				for i, v in pairs(game.Players:GetPlayers()) do
					local char = v.Character
					local torso = char:FindFirstChild("Torso") or char:FindFirstChild("HumanoidRootPart")
					torso.CFrame = CFrame.new(game.Workspace.TelePart.Position)
					--not this loop

				end
				text.Value = "Round is over!"
				wait(1)
				deb = false
				break -- this will break the first loop
			end
		end --this is the end of that first loop
	end