While loop gets stuck with "continue", how to fix?

Ok so the problem thats confusing me here is that im using a while true do loop but it gets stuck repeating the same line of code where “continue” appears!

Shouldnt it continue on to the other piece of code under it then break on the other if statemet?

Ex:

local growPart = game.Workspace:WaitForChild("GrowPart")

growPart.Size = Vector3.new(1, 1, 1)

while true do 

	if growPart.Size == Vector3.new(50, 50, 50) then  ----->>>>> MINES 
		growPart.Color = Color3.fromRGB(0, 157, 255)
		print("stopped loop!")
		--task.wait(1)
		continue
		--break
	end	
	
	if growPart.Size == Vector3.new(100, 100, 100) then 
		growPart.Color = Color3.fromRGB(255, 0, 179)
		print("loop stopped again!")
		break
	end
	

	growPart.Size += Vector3.new(1, 1, 1)

	task.wait(1)

end

It keeps printing “stopped loop” repetitively, why?

The use of continue isn’t actually how you imagine it - break is when you want to stop the loop entirely (in this case the while loop will completely end), and continue is when you want to stop this current iteration of the loop but keep the while loop running and skip to the next step.

This is easier imagined with a for i loop, in example:

for i = 1, 10 do
     if i == 3 then
          continue
     elseif i == 7 then
          break
     end
     print(i)
end

Output —>
image
As you can see, number 3 was skipped but the for loop keeps running until I get to 7 where I then break the loop, despite initially telling it to run up to 10.

If you want the code underneath the statement with the ‘continue’ in it to run afterwards, removing the continue from the first statement should amend your script :slight_smile:

yeah i did it like this afterwards!

How does this “continue” thing wok then exactly, does it just skip? [ ima look at doc ]

Ok. Put simply, continue says “don’t run anything in this loop below me and start again.”

So your code does this:

Sets a part’s size to 1,1,1

Every second increases its size by 1,1,1

Once its size is 50,50,50, it changes its color but continues the loop.

Once its size is 100,100,100, it changes the color again but completely stops the loop.

If this is not the behavior you intended for it, then we have to change your code. If this is what you wanted but it’s not working, then what exactly is happening? Specifically to the part’s size, color, and the print statements.

Well, if i leave continue as it is , it repeats that same code from where continue ends and it prints “stopped loop” multiple times, and if i dont use a task.wait it will crash

Ok. Let’s start from here then. What is your goal for what happens to the part?

And then also, which code are you using right now. The first one you showed or the second one?

well im using the second code i showed just kus that solved the issue, but i feel that the way i did it, doesnt involve continue, like it doesnt do anything, so i want to make sure i use continue the right way

So yes, in this case, continue wouldn’t be necessary.

All continue does is end the loop early and start over, if that makes sense.

So for example, if we write:

while true do
    wait(1)
    print("1")
    continue
    print("2")
end

What happens is that it prints 1, but then it hits continue and goes to the start of the loop and restarts, printing 1 again. This code will never print 2.

What continue is useful for is something like this:

Let’s say you want to print just 1 the first 10 times and then start printing both 1 and 2.

Then you could do:

x = 0
while true do
    wait(1)
    print("1")

    x = x + 1

    if x < 11 then
        continue
    end

    print("2")
end

So in your case, you could probably just not use continue.

Hope this helps!

1 Like

Btw I gtg now, but if you still need help, I can help later

oh ok thx! ill be re reading that code u sent lol to understand it better

1 Like

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