Coroutine wont stop

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I just want to know why this isn’t working.
  2. What is the issue? Include screenshots / videos if possible!
    It is supposed to yield the function for 5 seconds but the loop keeps going anyway.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I saw you can only use yield in the coroutine. Is this why? Is there a way to use it outside?
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local function SpawnParts()
	while true do
		local part = Instance.new("Part")
		part.Position = Vector3.new(0,0,0)
		part.Parent = workspace
		print("Looping")
		task.wait(1)
		
	end
end

local Coroutine = coroutine.create(SpawnParts)

coroutine.resume(Coroutine)
task.wait(2)
print("Pause for 5 seconds", coroutine.status(Coroutine))

coroutine.yield(Coroutine) -- Its supposed to stop for 5 sec
task.wait(5)
coroutine.resume(Coroutine)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

local yielded = false

local function SpawnParts()
	while true do
		local part = Instance.new("Part")
		part.Position = Vector3.new(0, 0, 0)
		part.Parent = workspace
		print("Looping")
		task.wait(1)
		if yielded == false then
			coroutine.yield()
			yielded = true
		end
	end
end

local Coroutine = coroutine.create(SpawnParts)

coroutine.resume(Coroutine)

task.wait(5)

coroutine.resume(Coroutine)

coroutine.yield() only works inside the function scope. If you want to use it outside you might need to use variables or alternative to coroutines.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.