Pause while loop while running

No, I’m not trying to “break” the loop. I’m trying to temporarily pause it then resume it later.

I’m attempting to make an ELS (Emergency Lighting System) system, however when I want the lights to turn off I want to make the lights turn off immediately. However, before it does, the full lighting script plays out before it turns off. This is a big problem with lights that have longer wait times.

Script: (script of only one of the lighting groups)

My ELS system when turned off: (slight delay in different groups due to longer wait times)

Goal: (to be turned off all at once at the same time)

2 Likes

Not sure if this is what you are looking for

repeat
task.wait(1)
until value.Value == true
2 Likes

No, I want a way for the scripts inside to immediately come to a halt once a value is turned on, similarly like the “break” line, but I want it to only be temporarily stopped.
Once the “value” is turned on, whatever in the script you provided will only stop after 1 second. I want that to be immediate.

1 Like

You can have a task.wait() instead of task.wait(1)

1 Like

If that was the case then all the light patterns would be messed up and go by extremely fast

1 Like
while game.Workspace.ELS.Value == true and game.Workspace.Airhone.Value == false then
-- code
end
1 Like

With this script, the only thing changed is detecting if the values were on, now the goal is that if there was a light pattern that lasted 1 second before changing, and the lights were to be turned off before the 1 second gone by, that light would be turned off immediately without needing to wait for that line of script to run.

1 Like

add a bool value under the script and inside the script check if the val == false the pause until it becomes true

this way you can access the bool value from another script and can pause or resume as you wish.

or use attribute instead!

1 Like

You could use the GetPropertyChangedSignal for the boolvalue and a variable controls the status of the loop. The reason for the loop ID is to catch any cases where multiple while loops were created due to all of the waits in the while loop.

I haven’t tested the code but I hope that this would work in your situation.

Edit 3: I just realized how bad the last code was but we can make use of threads and cancel them so that the loop will immediately terminate and will ignore any yields since your ELS system had a lot of waits

local ELS = BoolValue

local thread = nil
ELS:GetPropertyChangedSignal("Value"):Connect(function()
	if ELS.Value == true then
		
		thread = task.spawn(function()
			while true do
				task.wait(0.25)
				print("ELS ACTIVE")
				-- lighting system code
			end
		end)
		
	elseif ELS.Value == false then
		
		print("deactivated")
		task.cancel(thread)
		-- turn off lights
		
	end
end)

thank you, exactly what i was looking for!

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