Wait() freezing the entire code?

Hello everyone.
The script I’m showing below is my script’s issue: wait() freezes the entire script.

This script should be worked properly, but when I just set it to wait(), the script will stop working and the output shows “bfr wait”.

Do you know what’s going on through this script? Thank you.

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")
			wait()
			print("after wait")
			BodyVelocity.Parent = nil
			BodyVelocity:Destroy()
		end))
	end

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?

maybe try changing it to wait(0)

I don’t think anything is sabotaging to this function. There’s no errors of course, making this script function until it prints (“bfr wait”).

Also, That wait() function causes more than 15 seconds, which is too long than I excepted.
Ping is less than 50ms, which it shouldn’t occur.

wait(0) doesn’t work also.

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

are you having some kind of fps drop or something? because the for loop will instantly end, then if there are any codes after it, it will run

You should read the documentation before making suggestions.


As you can see, adding an argument is useless, since the minimum wait time is 29 milliseconds and there’s already a default.

No it won’t! The for loop will repeat that many times unless:

  1. An error occurs inside of it.
  2. You use the return or break keywords within it.

It actually worked as wait() does! Now, I’m gonna deal with deleting all of BodyVelocity.

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.

i know this, i am just testing if it will work. Im just curious

what i mean by that is it will end very very fast, I tested it and got 1e-05 up to 9e-05

There’s no need to “test”; follow the documentation’s advice.

Yes, that’s kind of the point? If you want the code to run slower, you can always yield it.

Maybe try using wait(0)

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")
		wait()
		print("after wait")
		BodyVelocity.Parent = nil
		BodyVelocity:Destroy()
	end))
    wait()
end

Because all loops needs wait() or wait(0) at end.

Check the past replies, doing wait(0) is pointless.

The only reason you’d want to add an argument is if you want your code to be more verbose, but that’s about it.

ok sure whatever,

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.

how does roblox do yielding stuff, i cant do that in lua

Did you mean to have coroutine.yield() where you have a wait() statement?