Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
4

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

Asked by 6 years ago

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?

0
Why not remove the while loop?? saSlol2436 716 — 6y
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. hiimgoodpack 2009 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago
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.)

0
No. I want the user to be able to decide when the code stops without using break. hiimgoodpack 2009 — 6y
0
Oh.... saSlol2436 716 — 6y
0
Yeah i am back in a while Animescapetower 10 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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()

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?) hiimgoodpack 2009 — 5y
0
ok Animescapetower 10 — 5y

Answer this question