Handling Coroutines Properly

  1. What do you want to achieve?
    I want to be able to create new coroutines without a large amount of memory leak. Also, I have holes around this topic I need to fill in.

  2. What is the issue?
    There are some gaps I need to fill in before I am confident in replacing my core game loop system.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have attempted to do so, but they have not answered what I am looking for.

-- Question 1: Will there be any problems if I were to do this?

-- The following code will close the thread if the event is fired.
	-- Do some code here
	-- Finish code here

Question 2: Am I required to set my coroutine to nil to save memory, 
or should closing the thread by itself be fine?

Quick update, the first question I realize I was using the code wrong, it should be the following:

local thread = coroutine.create(function()
	-- Do some code here
	-- Finish code here

The second question still remains unclear

While there is nothing wrong with nesting an RBXScriptConnection in the way you did, it might be best to connect the script event somewhere else.

You shouldn’t experience any major performance changes unless you are performing large amounts of computation. If you really want to try to conserve performance, reserve coroutines for things that MUST be ran synchronously. You shouldn’t worry about setting variables to nil unless the value must be overridden. Roblox natively uses collectgarbage() to cleanup unused variables.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.