Running parallel functions that wait for each other until done

ok so I kinda wanna run 4 parallel functions that each wait for each other before continuing. how would I go about this.

uwu

1 Like

You can Clone() and set the position then run a function.
``-- I’m not fully sure about this cause I’m sleepy now
local function O()

game.Workspace.Part:Clone()
– Sorry im tired
end
O()

Use coroutines and each one wait for four physical variables to be set to true.

local finished = {false,false,false,false}

function passSomeTime(myNumber)
   local myWaitTime = math.random(5) -- max 5 seconds wait time
   wait (myWaitTime)
   print "I finished!"
   finished[myNumber] = true
end

for i = 1,4,1 do
   coroutine.wrap(function() passSomeTime(i) end)() -- a subroutine will execute on its own
end

while wait() do
   for _,bool in ipairs(finished) do
      if bool==false then continue end
   end
   break -- we are done
end

print "Everyone is done!"

Key Takeaways of this Code

  • Use coroutines so they can all fire parallel of each other
  • Use some variable instance whether locally in the script or an outside instance to determine if all the subroutines are done
1 Like

I don’t think we need coroutines? It to connect scripts

sorry I think you misunderstood the question
what I meant is that 4 functions run in parallel (utilizing the new parallel lua) then wait until they are all done then proceeding to the next part of the script.

also sleep is good C:

uwu

1 Like

From what I understood it’s functions. I’m assuming within one script but same concept could be applied if you clone the script, then each script sets a respective variable to true when its done, then have an outside source checking when all the 4 variables are set to true to continue.

1 Like

Ah I see. You want to multithread. I’m a bit inexperienced in parallel programming right now (taking a course in that this coming quarter actually), but as far as I know it’s only a preview right now? Only resources I could find for you is this.