Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does this coroutine in the function not stop when I use return in the function?

So I am making a script, and I was testing if something I needed in my script works. So here is what I did to test.

function f()
	local c = coroutine.wrap(function()
		while wait(1) do
			print('yay')
		end
	end)
	
	c()
	
	wait(5)
	return
end

f()

It prints "yay" more then 10 times. What would I do to yield a coroutine outside of the coroutine, and why does using return on the function the coroutine is on not stop the coroutine?

2 comments

0

Why not remove the while loop??

0

It's an example for something else I will do. The wait(5) and return should still run, since while loop is in coroutine.

2 answers

local c = coroutine.wrap(function()
 	while wait(1) do
       print('yay')
		break
    end
end)
local function f()
	wait()	
	return c()
end
f()

Hopefully, this is an answer that should work (I'm not the best with coroutines.)

3 comments

0

No. I want the user to be able to decide when the code stops without using break.

0

Oh....

0

Yeah i am back in a while

The reason you can make it stop is to make it a for anystringhere = 1,40 do -- code here end

like this for example

stop = true
function f()
    local c = coroutine.wrap(function()
if stop == true then
        for n = 1,40 do
 wait()
            print('yay')
        end
stop = false
end
    end)

    c()

    wait(5)
    return
end

f()

2 comments

0

No, the question is that why does it not stop the coroutine? Anyways, I think I sorta understand why it does that. (Also, why are you answering an old question?)

0

ok

This discussion is preserved as it was. New answers, comments, and votes are closed.