"Pause" Code Until Critera is Met?

Hello devs! I’m in need of a bit of help again.

Recently I’ve started developing a small little zombie game where players have to move through a series of rooms and reach the end to win. However, I wanted to implement a system where the game “pauses” until something is done (i.e. specific zombie is KOed, door is manually opened) but I couldn’t find anything that could help me in the forums.

Is there any small trick that anyone knows on how to pause/loop code at a specific point until specific criteria is met? If so, it would be a lot of help!

are look looking for a repeat loop?

repeat task.wait()
    CodeHere
until Condition

should repeat the code until the condition is met

You could either do an if then statement where you can have a variable that’s gonna be true when the criteria is met, and when it is not nothing would happen, something like this:

while wait() do
     if CONDITION then
         -- do something
     end
end
--OR
game.RunService.Heartbeat:Connect(function()
     if CONDITION then
          -- code here
     end
end)

You could also use

repeat task.wait() until CONDITION -- (keep in mind this will only work once and then the code after will run forever unless you add another repeat until loop)
-- code that will be ran after the variable is true 

OR

This isnt the best idea, but you can disable the script with the code you want to pause and then re enable it in another script after the condition is met.

The optimal way:

Use custom Signals and Janitor.
You’ll need to understand also coroutine.yield, coroutine.running and task.spawn.

Use the custom Signals to detect when something happens, these signals will usually just be fired when that condition is met.

You’ll use Janitor to clean up any objects, connections that you won’t need anymore.

Use c.yield and t.spawn to yield and resume that thread.

1 Like

Alright! Nisam’s code example did help me figure out how to pause a piece of code, but I might look into the Custom Signals and Janitor stuff in the future (perhaps when I clean up my game’s code?)

But I thank you all for the help! It really means a lot!

This actually isn’t ideal. You should always strive to write code yields into events instead of loops (unless you’re explicitly waiting for any amount of time or your system repeats).

Provided OP’s zombie has a Humanoid, you can write Zombie.Humanoid.Died:Wait() to the exact same effect at a fraction of the cost in memory.

See event yielding and GetPropertyChangedSignal.

1 Like

Ohhh, I didn’t know you could use wait() like that. Sorry about that. These two articles do look like a ton of help though!

wait(var : number) is a different function entirely from Event:Wait() which is worth knowing. wait() is subject to be deprecated at any point in time in favor of task scheduler yielding. They just share the same name because they exist in differing packages/source containers within Roblox top-level libraries which means that the names do not clash.

Additionally, wait(var : number) immediately indexes for easy access, whereas task scheduler yields from task library (and should be used over wait()).

print(task)

--[[
                    ["defer"] = "function",
                    ["delay"] = "function",
                    ["desynchronize"] = "function",
                    ["spawn"] = "function",
                    ["synchronize"] = "function",
                    ["wait"] = "function"
]]
2 Likes