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

exploring ways to not start a function until one finishes

1 Like

yeah, promises are used for exactly that

1 Like

function1()
function2()

function2 dutifully waits on function1().

2 Likes

Um excuse me sir but function2() would run immediately after function1() and if function1() doesn’t yield on the tween it will mess up :heart:

2 Likes

you can use Promises for this situation:
roblox-lua-promise | roblox-lua-promise (eryn.io)

i’ve only used promises while using remote functions in knit. would it make sense to use a promise in this situation? where i want to make sure i’ve done alls i need to do for each player (my for loop is finished) before moving on… right now i am using a repeat wait until

what if there is no tween on function one?

If you’re using a for loop it will only continue once it has completed everything inside, you can guarantee that nothing will overlap or happen prematurely with

for _, player in players do
    -- ...
end

Alternatively if you’re doing something that requires some time, you could count the number of players, make a new variable with the value of 0 and increment it by 1 each time the action has completed for each player, and use task.wait until both numbers are the same

local playerCount = #players
local completed = 0

for _, player in players do
    task.spawn(function()
        -- ...
        completed += 1
    end)
end

repeat
    task.wait()
until completed == playerCount

Either this or using events will be the best approach