Breaking while loop doesn't stop it

Hey, I am kind of stumped right now because using a break doesn’t stop the loop. I have tried using a RenderStepped connection and disconnecting it but that didn’t work either. It is strange because normally it always stops whenever I use the same method. Here is a snippet of my code:

function startMoving(coin)
	while true do
		if coin and destroyedParts[coin] == nil then
			move(coin)
		else
			break
		end
		
		wait(0.1)
	end
end

Could it be that roblox is down right now? I have seen other posts describing weird errors but on the status page it says everything is fine.

This is bascilly saying “if the coin is nil” therefor your 2nd argument will return false

replace it with “if not coin”

Break only breaks out of the first code block around it. In this case, it might be breaking the if statement. You’ll need a variable and set the while loop to while variable == true and set it to false in the else

1 Like

Ah okay. I never knew that. Will roblox automatically break the loop if the conditions are not met. If not, how would I do that without using another if statement (because that would lead to the same issue)?

a break inside an if statement will still break the loop outside of it
EDIT:

while true do
	break
end
print("ok")
while true do
	if true then
		break
	end
end
print("ok")

both of the prints run
this is easy to test

2 Likes

Yeah, that was what I thought. Do you have any idea what might be wrong? My best guess is that it is because it is in a function.

what are you putting in for the Coin variable?

coin must be a false value or destroyedParts[coin] must be a true value to break the loop.
If coin exist and you find “coin” in this table, so the loop wont break. Could you show a print of the function move(coin)? So we can check the modifications and fix.

The coin is an instance. The function is called when the player is close to the part.

The thing is that I put a print over the break to see if it would register the coin being nil. It printed perfectly, so I am pretty sure that is not the problem.

So, can you show when the function startMoving(coin) is called?

If you are calling a loop everytime it is near, it would cause a memory leak. There’s probably a ton of loops running at once.

May I ask what should this function or while loop do to this coin?

I could provide a better way of the attempt you trying to do if possible. :slight_smile:

Looks like the coin is moved into the table destroyedParts and breaks the loop, but break isnt working

1 Like

but if the function startmoving() is called many times, so you have some loops in the game and even if 1 breaks, the others may keep looping

Aha, I got it.

You should use spawn() to prevent loops in the function from delaying other functions that are mean’t to be called out before startmoving()

For Example
When Not Using Spawn()

while true do
wait()
end

print('Spawn') --This will NOT print since the while loop is delaying that.

With Spawn()

spawn(function()
while true do
wait()
end
end)

print('Spawn') --This will NOW print.

Questions?

Thats the why I want to see when this function is called

task.spawn() should be used instead of spawn()

1 Like

Fixed it myself. I switched to a renderstepped event and disconnected it when the conditions were met. For some reason I had to manually set the coin to nil but hey, it works. Thank you all for the help!

1 Like

How would this prevent it though? The loop would still play on. :thinking: