Why is this while loop not breaking when it tell it too?

Hey!
Why when I set readyForFullSpin1 to false in another function it makes it false (I know this as after each tween in the loop I print it out) but the loop seems to carry on as it keeps on printing false and carries on with the tweens. Does anyone know what I am doing wrong as I can seem to see anything wrong.

Code:

spinModule.spin1 = function()
    wait()
    print("RockingTug System V1: Attempting to start spin...")
    wait(1)
    if spinModule.isRideSpinning then
        print("RockingTug System V1: Attempt Success. Starting Spin.")
        readyForFullSpin1 = true 
        wait(swingTime+4.5)
        spinSTART:Play()
        while readyForFullSpin1 do
            wait()
            spinSTART.Completed:Connect(function() spin180:Play() spinOn180 = true print(readyForFullSpin1) end)
            spin180.Completed:Connect(function() spin90:Play() if spinOn180 == true then spinOn180 = false spinOn90 = true print(readyForFullSpin1) end end)
            spin90.Completed:Connect(function() spin0:Play() if spinOn90 == true then spinOn90 = false spinOn0 = true print(readyForFullSpin1) end end)
            spin0.Completed:Connect(function() spinMINUS90:Play() if spinOn0 == true then spinOn0 = false spinOnMINUS90 = true print(readyForFullSpin1) end end)
            spinMINUS90.Completed:Connect(function() spin180:Play() if spinOnMINUS90 == true then spinOnMINUS90 = false spinOn180 = true print(readyForFullSpin1) end end)
            
            if not readyForFullSpin1 then
                wait()
                print("RockingTug System V1: Stopping Spinning")
                break
            else
                return
            end
        end
    else
        print("RockingTug System V1: Attempt Failed.")
    end
end

What’s the point of having a while loop? Your code within it only runs once, as you either break it or return it. The problem most likely lies within one of your events.

It does not run once?
It runs that code forever so it spins forever until I change the readyForFullSpin1 variable in another function in the module script. I know it changes as it prints true/false in the loop that is also another way I know it is doing the loop.

I have done it like this before and it is working all fine but I am unsure why this one is not working.

Well it should, you’re either breaking the loop or returning the function - either or will effectively stop the loop.

if not readyForFullSpin1 then
   wait()
   print("RockingTug System V1: Stopping Spinning")
   break -- stops the while loop
else
   return -- returns the function
end
1 Like

That exactly what I have… but it is not working. But I know the readyForFullSpin1 is set to false.

I don’t really know what the problem you’re having is because it physically cannot loop again, but once again is there a need for having a while loop, since it only loops once.

It does loop again…

I have done it now anyway. Thanks for trying to help :slight_smile: