Can i use coroutine.yield() in a regular function?

I want to use coroutine.yield() in a regular function, is there someway i could do this without creating coroutine function.

A couroutine works because it acts as a pausible task to be resumed by the task scheduler. Why don’t you want to make a coroutine?

Also you can use coroutine.yield, but it will yield the current function for a single execution frame and that’s it. People used to use it to wait for 1/60 seconds before RenderStepped existed.

can i make a pausible function

Example:

local Printer = function()
      print("1")
      coroutine.yield()
      print("2")
end
Printer()
Printer()

Yes but you’ll just yield the current thread of execution.

coroutine.yield()
print("Hello world!") --Never prints.

Use coroutine.create/coroutine.wrap to create coroutines which you can yield.

local function f()
	print("Hello")
	coroutine.yield()
	print("world!")
end

local wrapper = coroutine.wrap(f)
wrapper() --Prints 'Hello' immediately.
task.wait(1)
wrapper() --Prints 'world!' after one second.
2 Likes

i Know what coroutine are but i want to yield a function untill it is called again

Right, but a coroutine is a function. Why can’t you use coroutines?

1 Like

what exactly are you trying to do with this? the task library might be your friend here paired with some signals. you can do something like:

function doThing()
    local running = coroutine.running()

--code
    local a,b,c = ---who knows

    Signal:Once(function()
        task.spawn(running, a, b, c)
    end)


    return coroutine.yield()
end

which would run some code, and yield until Signal is fired and return whatever you pass after running in task.spawn

I used coroutine, it is the easiest solution yet.