Temporarily Suspend Execution of a Thread

I am making a elevator that simply moves up and down using tweens in a while condition do loop (the while loop loops every 90 seconds). I want it so a bool value in replicated storage named “blackout” is switched false, the thread that makes my elevator move up and down will be paused, or temporarily suspended of execution. Originally I thought of wrapping my while loop in a function and then run it as a coroutine, and that you could just do coroutine.yield(function) outside the function itself, but it is illogical and it will not work at all. Is there anything I can do to achieve the effect I want?

Every iteration of the loop, you could do this:

if blackout.Value then blackout.Changed:Wait() end

Basically, this means that if blackout’s value ever switches to true, then the script will stop running until blackout switches to false again.

Roblox guarantees that the Changed event of a BoolValue only fires when its Value changes.

Sadly, my iteration loops every 90 seconds, so this will not work. Any other workaround you can think of?

There’s no way to do what you ask without the upcoming task.cancel which isn’t released yet. Refactor your code to use timers that get updated every frame, then you can simply pause them during your blackout.

When task.cancel does come out, you can spawn another coroutine that basically “watches” the blackout value for changes. When it detects a blackout starts, it can task.cancel the timer coroutine, which basically means the wait call will never finish on its own. Then, once the blackout is over, it can use task.delay(remainingSeconds, coro) to resume the timer thread, where remainingSeconds is the time left in the wait that was cancelled. (you can calculate this by storing the current time in a variable immediately before each wait starts)

Okay, that explanation is getting a little complicated, so let’s just leave it at “this isn’t possible right now”. Fix the root cause (the X problem) by switching from waits to timers. You can update the timer each Heartbeat.