Syntax
local nextItem = coroutine.wrap(function() coroutine.yield(item) end)Examples
Luau coroutine producers
Run as a standalone current Luau script, or as a Script in Roblox Studio ServerScriptService. No Roblox engine APIs are required; type-annotated syntax is not standard Lua.
local nextSquare = coroutine.wrap(function()
for i = 1, 3 do coroutine.yield(i * i) end
end)
for value in nextSquare do print(value) end- Output: 1, 4, 9. When the coroutine returns no value, generic-for iteration stops.
Best practices
- Do not call a completed producer again. Errors propagate to the caller; use protected calls where failure is expected.
Runtime & versions
Luau host; Roblox engine examples require Roblox Studio. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
Luau documentation