Task.spawn or coroutine.wrap?

Hi guys, I want to make a function don’t yield, but I just feel like task.spawn() and coroutine.wrap() just takes memory every time they run, or they get stored somewhere, is this true?

What i’m doing is something like this;

local time = os.clock()

local function test()
print('hi')
task.wait(.5)
print('bye')
end

while (true) do
task.wait(.1)
   if (os.clock() - time) >= 1 then
         test() -- what do I add here, coroutine.wrap or task.spawn?
        time = os.clock()
   end
-- do other stuff here that cannot be because of test()
end

I think either is fine to use. I personally prefer just using the coroutine.wrap() method and I believe task.spawn() utilizes coroutines any how. And they should free up their memory when the function ends.

What are you trying to achieve?

Exactly what is mentioned in the code.

The post isn’t really very clear other than making a function not yield.

local time = os.clock()

while (true) do
task.wait(.1)
   if (os.clock() - time) >= 1 then
       local doTest = coroutine.wrap(function()
            print('hi')
            task.wait(.5)
            print('bye')
       end)
       doTest()
       time = os.clock()
   end
   -- do other stuff here that cannot be because of test()
end

yes, I just feel the functions doesn’t gets cleaned or coroutine.wrap takes memory that doesn’t gets ‘cleaned’
so I can use as many as coroutine.wrap as I want?

Well its stored into the local variable, so the moment the function returns after it finishes their should be no more reference to it. The garbage collection should clear that memory for use at that point. That’s why I asked what exactly your trying to achieve (overall). Often times we can provide a better way of achieving your goal than the way your attempting.