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!
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.
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.
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()).