I heard of something called coroutine and I checked the documentation but I am still confused. How could I skip the wait time for a function (because it takes like 20 seconds and I want it to happen simultaneously) in a for in pairs loop?
Here is the loop:
for index,stem in pairs(stems) do
grow(stem)
wait(math.random(10,20)/100)
end
for index,stem in pairs(stems) do
task.spawn(function
grow(stem)
wait(math.random(10,20)/100)
end)
end
for index,stem in pairs(stems) do
coroutine.wrap(function
grow(stem)
wait(math.random(10,20)/100)
end)()
end
You can use coroutines/task.spawn() to achieve this. Both create a new thread to handle the execution of code while the main thread continues with the execution of the script.
for index,stem in pairs(stems) do
task.spawn(function()
grow(stem)
wait(math.random(10,20)/100)
end)
end
for index,stem in pairs(stems) do
coroutine.wrap(function()
grow(stem)
wait(math.random(10,20)/100)
end)()
end
I apologise, I forgot to add () to each function body inside both task.spawn() and coroutine.wrap() calls.