Best Way to Wait for Condition or Time

Hi, I am trying to figure out what is the best way to halt my code until either a certain amount of time has passed or a condition has been met.

Here is the pseudocode for what I am currently thinking, but I am not sure if this is the best way to do so.
while conditionNotMet and os.difftime(os.time(), startTime) ~= timeInWholeSeconds do
wait()
end

and maybe for a more specific time constraint (partials of seconds), you could spawn another function that waits the specific time and sets a boolean value that is part of the original while loop.

Forgive me if the solution is simple.

Edit: Cleaner version could be
repeat wait() until (conditionMet or os.difftime(os.time(), startTime) == timeInWholeSeconds)

1 Like

Why do you need to “halt” your code? There’s no real reason to just stop your code from running at an arbitrary point. Need some context

1 Like

There should not need to be a reason. I am just inquiring what the best way to do that would be. It could do something such as wait until an objective is complete or until the players are out of time.

The best way depends on the context, so yes there does need to be a reason.

1 Like

In that case I would make a timer and a bindable event that fires when the objective is complete or something similiar (just imagining at the moment). It all depends on the context really.

2 Likes

If you want to wait for a certain objective to be met, then create an event that fires once that objective is complete, and bind it to a function which runs the code you want to execute afterwards. Really I would need to see a specific example to give any sort of real answer.

1 Like

wait will run even if the condition is true, so it will act differently than the while loop.

Thread related to this: Avoiding wait() and why

If you can set up an event that will fire instead of using a loop, that would probably be the best.
To make it resume after X seconds, you would probably just manually resume it later if it wasn’t already resumed.

4 Likes