How to stop and resume this while loop manually?

But now to get back to what the real issue of the code is here. Starting a loop that has a delay between checks like this and stopping it at the press of a button is a bit more complicated and a boolean debounce can create issues if it’s flipped 2x before the loop can check it again (in this case it only checks the condition in the while loop every 2 seconds)

The way I got around this in my code is actually storing the tick the loop was started at and comparing it to that. That way upon the check it will realize it’s incorrect. That’s essentially what this code is doing before entering the while loop

fireworksEnabled = not fireworksEnabled
	
newLoopTime = tick()
local loopTime = newLoopTime --This is all setup to allow the loop to only continue on the last hit

That way if you call the function again, the currently running loop will recognize there is an updated loop and stop itself.

That is the hidden issue in your code. The issue that prevented it from working at all though is actually just a simple mistake.

script.Parent.MouseButton1Down:Connect(function()
	Firing.Value = true
	while Firing do
		Fire()
		
		if not Firing then
			break
		end
	end
end)

You are only checking if Firing exists. It does exist as a child of workspace so you are saying something no different than true when you just type Firing. You should be checking Firing.Value.

while Firing do --should be while Firing.Value do
if not Firing then --should be if not Firing.Value then
1 Like

You can do this with the code you originally have. This is just a suggestion, but I believe you can use coroutines to solve your problem. First, you should make your Fire function a coroutine by doing local fire = coroutine.create(function(). I recommend that the code below the coroutine be already a while loop. With that the function is not running, so you will have to put coroutine.resume(fire) somewhere in your script. In your mouse button 1 down event, you can say after Firing.Value = true,
if Firing do coroutine.resume(fire)
and then
elseif not Firing then coroutine.yield(fire)

Once again this is just a suggestion and I am not sure if this will work.

You can learn more about them here:

2 Likes

Hi there! I believe the easiest option to do here is just a break function. I believe this to be so as the loop was essentially never told to stop after the button was first pressed, putting a break in the loop at the end would stop it until the button is pressed once more.

script.Parent.MouseButton1Down:connect(function()
	while true do
			game.Workspace.Fire.S4.ParticleEmitter1.Enabled = true
			game.Workspace.Fire.S4.ParticleEmitter2.Enabled = true
			game.Workspace.Fire.S4.SurfaceLight.Brightness = 10
			game.Workspace.Fire.S4.Sound:Play()
			wait(0.5)
			game.Workspace.Fire.S4.ParticleEmitter1.Enabled = false
			game.Workspace.Fire.S4.ParticleEmitter2.Enabled = false
			game.Workspace.Fire.S4.SurfaceLight.Brightness = 0
			game.Workspace.Fire.S4.Sound:Pause()
			game.Workspace.Fire.S3.ParticleEmitter1.Enabled = true
			game.Workspace.Fire.S3.ParticleEmitter2.Enabled = true
			game.Workspace.Fire.S3.SurfaceLight.Brightness = 10
			game.Workspace.Fire.S3.Sound:Play()
			game.Workspace.Fire.S5.ParticleEmitter1.Enabled = true
			game.Workspace.Fire.S5.ParticleEmitter2.Enabled = true
			game.Workspace.Fire.S5.SurfaceLight.Brightness = 10
			game.Workspace.Fire.S5.Sound:Play()
			wait(0.5)
			game.Workspace.Fire.S3.ParticleEmitter1.Enabled = false
			game.Workspace.Fire.S3.ParticleEmitter2.Enabled = false
			game.Workspace.Fire.S3.SurfaceLight.Brightness = 0
			game.Workspace.Fire.S3.Sound:Pause()
			game.Workspace.Fire.S5.ParticleEmitter1.Enabled = false
			game.Workspace.Fire.S5.ParticleEmitter2.Enabled = false
			game.Workspace.Fire.S5.SurfaceLight.Brightness = 0
			game.Workspace.Fire.S5.Sound:Pause()
			game.Workspace.Fire.S2.ParticleEmitter1.Enabled = true
			game.Workspace.Fire.S2.ParticleEmitter2.Enabled = true
			game.Workspace.Fire.S2.SurfaceLight.Brightness = 10
			game.Workspace.Fire.S2.Sound:Play()
			game.Workspace.Fire.S6.ParticleEmitter1.Enabled = true
			game.Workspace.Fire.S6.ParticleEmitter2.Enabled = true
			game.Workspace.Fire.S6.SurfaceLight.Brightness = 10
			game.Workspace.Fire.S6.Sound:Play()
			wait(0.5)
			game.Workspace.Fire.S2.ParticleEmitter1.Enabled = false
			game.Workspace.Fire.S2.ParticleEmitter2.Enabled = false
			game.Workspace.Fire.S2.SurfaceLight.Brightness = 0
			game.Workspace.Fire.S2.Sound:Pause()
			game.Workspace.Fire.S6.ParticleEmitter1.Enabled = false
			game.Workspace.Fire.S6.ParticleEmitter2.Enabled = false
			game.Workspace.Fire.S6.SurfaceLight.Brightness = 0
			game.Workspace.Fire.S6.Sound:Pause() 
			game.Workspace.Fire.S1.ParticleEmitter1.Enabled = true
			game.Workspace.Fire.S1.ParticleEmitter2.Enabled = true
			game.Workspace.Fire.S1.SurfaceLight.Brightness = 10
			game.Workspace.Fire.S1.Sound:Play()
			game.Workspace.Fire.S7.ParticleEmitter1.Enabled = true
			game.Workspace.Fire.S7.ParticleEmitter2.Enabled = true
			game.Workspace.Fire.S7.SurfaceLight.Brightness = 10
			game.Workspace.Fire.S7.Sound:Play()
			wait(0.5)
			game.Workspace.Fire.S1.ParticleEmitter1.Enabled = false
			game.Workspace.Fire.S1.ParticleEmitter2.Enabled = false
			game.Workspace.Fire.S1.SurfaceLight.Brightness = 0
			game.Workspace.Fire.S1.Sound:Pause()
			game.Workspace.Fire.S7.ParticleEmitter1.Enabled = false
			game.Workspace.Fire.S7.ParticleEmitter2.Enabled = false
			game.Workspace.Fire.S7.SurfaceLight.Brightness = 0
			game.Workspace.Fire.S7.Sound:Pause()
            break
	       end
end)
1 Like

Hey, sorry for the late answer, I had a bit of a busy 2 days lol.
You have no idea how much I appreciated your answers! The explanations were pretty clear and helped me a ton to understand more about loops and tables. Seriously, thank you! :slight_smile: :heart:

2 Likes