What's good practice when it comes to waiting to start one task until another finishes?

I have a for loop that moves objects to a table of positions and once that is finished i want to change each object to another position one by one. Is there a way I can delay the second bit until the first bit finished? Not using task.wait, task.delay, or task.defer … unless those would be a good idea to accomplish this. But I’m wondering if anyone has any other suggestions.

14 Likes

Maybe create an event that fires once all the positions from the first bit are done being set

5 Likes

what about the loops being in the same function?

5 Likes

Just use task.delay where the first parameter is the tween time for the parts in the first group

4 Likes

Maybe a bit of code would help. If you have a for loop moving things why couldn’t you just start the second part when that for loop has completed?

4 Likes

not tweening. just changing cframe. but in lua i suppose the loop will have to finish before the next loop can run? so maybe it doesn’t matter and/ or can just use task.wait

2 Likes

that’s what i’m doing essentially but i am looking for a way to “make sure” it’s finished before proceeding. just in case.

1 Like

If you’re changing the cframe it’s the same idea, you should have a variable for how long it takes to complete so just use that

2 Likes

You’ll want to insert a delay between the two loops otherwise they’re just going to appear to happen instantly.

Make sure to mark this as solution too!

1 Like

changing multiple cframe with loop.

1 Like

so you mean a task.wait and i’m wondering about alternatives

2 Likes

Your loop should still have a timeout number which is the one you want to be using

2 Likes

Why do you want an alternative to task.wait()?

  1. You want to move the items from Position A to Position B
  2. You need some period of time for players to observe the items in position B.
  3. Then you want to move them to Position C

You need a task.wait() to handle step #2, otherwise it’ll just appear like A was moved to C.

3 Likes

so right now i’m just using a repeat wait until all the objects have been moved … then might put in a little wait after which i think will correspond with the camera tween

1 Like

i’m exploring alternatives outside of task.wait just to see what other methods exist. but i haven’t found any so far

1 Like

What do you mean “until all objects have been moved” – you’re not using a tween you said, you’re just setting the CFrame (:PivotTo() hopefully) directly aren’t you?

1 Like

yeah. the objects i’m moving i’m putting in a table while they are where they are. so i’m checking to see if the objects in the table are all the objects. then i will probably do a task.wait() after that or in my repeat i’ll have it to also check a bool value to let know that it should continue. because i want a delay as well as checking when all the objects are where they are supposed to be.

1 Like

It is hard to visualise without some code. I know how I would do this but that won’t help you because it probably wont look anything like your code. If you show the code I might be able to refactor it to make it do what you want.

1 Like

Maybe try using a promise? Still not sure what the question really is tbh

1 Like

this is just contemplating logic. not debugging or wanting to refactor existing code. but i’ve just ended up using a repeat loop until all objects are moved then a wait for delay until next loop starts.

1 Like