Are there any errors occuring in the output? The intended output should look like this:
bfr wait (x10)
after wait (x10)
Have you redefined the wait function anywhere in your code? Is there anything that is eating a LOT of processing power at the same time the script is running, causing wait() to yield for longer than expected?
It’s because you’re using the coroutine function.
Since you wrapped wait() inside coroutine function, it wouldn’t be yielding iteration at all, and it will cause the excessive use of memory in a short period.
To fix it, you could simply avoid using coroutine function or take wait() out of the coroutine function.
-- Way Avoiding Coroutine
for i=1,10 do
local Coin = Objects.Coin:Clone()
Coin.Parent = script.Parent.Parent
Coin.Position = script.Parent.PrimaryPart.Position
local BodyVelocity = Coin.BodyVelocity
BodyVelocity.Velocity = Vector3.new(math.random(-500,500)/100,50,math.random(-500,500)/100)
print("bfr wait")
wait()
print("after wait")
BodyVelocity.Parent = nil
BodyVelocity:Destroy()
end
-- Way taking wait() out of coroutine
for i=1,10 do
coroutine.resume(coroutine.create(function()
local Coin = Objects.Coin:Clone()
Coin.Parent = script.Parent.Parent
Coin.Position = script.Parent.PrimaryPart.Position
local BodyVelocity = Coin.BodyVelocity
BodyVelocity.Velocity = Vector3.new(math.random(-500,500)/100,50,math.random(-500,500)/100)
print("bfr wait")
end))
wait()
print("after wait")
BodyVelocity.Parent = nil
BodyVelocity:Destroy()
end
It seems waiting is doing nothing wrong. The culprit is most likely an error within your function, since coroutines not created with coroutine.wrap will never show their errors in the output, but still stop running. Try to run your code outside of a coroutine or use coroutine.wrap to find that error.
As @WilliamAlezandro said, if you want the loop to temporarily stop, you don’t need to use coroutines at all.
the reason I said instantly because it will end “instantly” or very very fast, even faster than what you can see. I mean like you can’t clearly see things move at 1cm / 0.00005s or 1px / 0.00005s
No need to repeat yourself. Code is ran as fast as possible unless you yield it with functions like wait. “will end instantly” implies the loop ends before looping all times, which isn’t the case here.
Let’s get back on-topic and help out OP with their problem.