Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is it possible to make this coroutine anonymous or do I have to use the variable?

Is it possible to make this coroutine anonymous or do I have to use the variable? Thanks!

while true do
	local newThread = coroutine.wrap(function()
		for i = 1, 10 do
			
		end
		coroutine.yield()
	end)
	newThread()
end

1 comment

0

You shouldn't be using coroutine for this. Use spawn

2 answers

✓ Accepted answer

All you have to do is call the result of coroutine.wrap directly.

while true do
	coroutine.wrap(function()
		for i = 1, 10 do

		end
		coroutine.yield()
	end)()
end

Hope this helped.

If you're not meaningfully[^1] using coroutine.yield and coroutine.resume (or, if you use wait() or :wait()) then you should not use coroutine at all.

If you want to start a "background thread", use spawn or delay. spawn starts a "thread" that will be started soon™. delay starts a "thread" that will be started after the specified amount of delay.

while wait(1) do
	spawn(function()
		-- do stuff
	end)
end

[^1]: in particular, you should call resume at least twice on the same coroutine to justify the use of coroutine.create over spawn

1 comment

0

Neat

This discussion is preserved as it was. New answers, comments, and votes are closed.