How can I get a returned value from a function which has a yield inside a coroutine.wrap ?
Example
local function test()
coroutine.wrap(function()
wait(3)
return "done"
end)()
end
local val = test()
print(val) -- Prints nil
wait(5)
print(val) -- Also prints nil
In this example, what should I do to be able to get the returned value of "done"? (I need the coroutine.wrap btw)
The variable val would be defined as the return value(s) of test which are nil. The return statement is returning something to the coroutine. You’re looking to use yield instead.
Variables don’t update automatically so I’m not exactly sure how you’d yield a value back after using a wait but in terms of an instant return:
local function test()
coroutine.yield("done")
end
local testCo = coroutine.wrap(test)
local val = testCo()
task.wait(5)
print(val) --> done