Question on clearing out coroutines

Hey developers, I was wondering what’s the best way to clear out a coroutine when it’s no longer needed.
I have some code which uses coroutines but I want the coroutine to be cleared out when its use is no longer needed. This is my code:

local modules = serverScript:FindFirstChild("Modules");
local prompt = require(modules.Prompt);

prompt.init();

local repl = game:GetService("ReplicatedStorage");
local remotes = repl:WaitForChild("Remotes");
local leave = remotes:FindFirstChild("LeaveStand");

leave.OnServerEvent:Connect(function(player)
	do
		coroutine.resume(coroutine.create(function()
			for index, value in pairs(player.Data.Stand.Value["PlayersReady"]:GetChildren()) do
				local countUI = player.PlayerGui.Countdown;
				countUI.counter.Text = "Ending operation...";
				wait(1);
				countUI.Enabled = false;
				value:Destroy();
			end
			prompt:remove(player, player.Data.Stand.Value);				
		end))	
	end
end)

Will Lua garbage collect this? If not, how can I make this get garbage collected?

Maybe check this article

2 Likes

Thanks, this solved my question.

1 Like