Looping fails to reset / restart all over

I seem to have a problem where it will repeat the same amount of times I clicked a button, and it seems to repeat the i variable and then repeat it like (2 times of clicking the button = 2+ prints.

I want to fix this but I’m unsure what to do
My code:

local Barriers = script.Parent:WaitForChild("Barriers")
local GameStatus = script.Parent.Parent:WaitForChild("GameStatus")
local Countdown = script.Parent.Parent:WaitForChild("Countdown")

local Value = false

GameStatus.Changed:Connect(function(NewValue)
	if NewValue then
		Value = true
		local CountDown = tonumber(Countdown.Value)
		Barriers.Barrier1.CanCollide = true;
		Barriers.Barrier2.CanCollide = false;
		while Value ~= false do wait()
			if Value ~= true then
				Barriers.Barrier1.CanCollide = true;
				Barriers.Barrier2.CanCollide = true;
				break
			end
			Barriers.Barrier1.CanCollide = false
			Barriers.Barrier2.CanCollide = false
			for i = tonumber(CountDown), 0, -1 do wait(1)
				if Value ~= true then
					Barriers.Barrier1.CanCollide = true;
					Barriers.Barrier2.CanCollide = true;
					break
				else
					print(i)
				end
			end
			Barriers.Barrier1.CanCollide = true
			for i = 15, 0, -1 do wait(1)
				if Value ~= true then
					break
				else
					print(i)
				end
			end
			Barriers.Barrier1.CanCollide = false
			Barriers.Barrier2.CanCollide = false
			for i = tonumber(CountDown), 0, -1 do wait(1)
				if Value ~= true then
					Barriers.Barrier1.CanCollide = true;
					Barriers.Barrier2.CanCollide = true;
					break
				else
					print(i)
				end
			end
			Barriers.Barrier2.CanCollide = true
			for i = 15, 0, -1 do wait(1)
				if Value ~= true then
					Barriers.Barrier1.CanCollide = true;
					Barriers.Barrier2.CanCollide = true;
					break
				else
					print(i)
				end
			end
		end
	else
		Value = false
	end
end)

I’d appreciate the help to figure out why it keeps repeating, I made sure the game resets and etc but it seems to repeat this.

They seem to be coming from the queue of the next for i loop, unsure of how I can possibly fix this though. Please let me know of anyway to fix this

Could you explain the application a bit? How many loops are running is confusing me to read. It seems like it’s having a dynamic time where the barriers don’t collide, then waits 15 seconds, then the same dynamic timer where the barriers are now colliding, but another 15 second timer to set them to collide again?
There may be an easier solution than addressing the loops overlapping is why I ask.
You could also debug which loop the output is coming from, like the first loop could print("A "..i), the next print("B "..i), etc.

Ah my bad, basically the problem is when it’s looping once I break that specific loop, it’ll go to the other loop and loop with the other one basically like this.

  • Loop1 (1st)
  • Loop2 (2nd)

Loop1 breaks then once I redo this part when resetting the game it’ll go to the Loop2 while Loop1 is restarting its code, then adds on a queue for everytime the game resets but these loops don’t like ‘break’ all it breaks is the fact it goes adding on with more prints each time the game was reset.

It seems annoying that the break function won’t do anything considering I want it to only reset the game (entirely so it works again)

Basically a refresh but it’s weirdly giving me more and more prints (that print the amount of times the game reset.)

for i = 10, 0, -1 do wait(1)
   print(i) -- if I were to let this have a break function it'll remove the queue of other loop then they'll both start together instead of awaiting its next turn.
   if value then
      break
   end
end

for i = 10, 0, -1 do wait(1)
   print(i)
   if value then
      break
   end
end

(Let me know if you need a more basic understanding I just woke up today, so I’m a bit just unfocused on stuff)