I have this code running in a UserInputService InputBegan function. Whenever the player clicks, this portion of the function will run. It creates a coroutine every time. Would this cause issues, with hundreds, even thousands of dead coroutines piling up? If so, how would I fix it?
if not coolingDown then
local shootTimer = coroutine.wrap(function()
wait(coolDownLength)
coolingDown = false
end)
plane.Events.Shoot:FireServer()
coolingDown = true
print("SHOWOO")
shootTimer()
end```
coroutine.wrap actually returns a function, and functions are objects so they are passed by reference. Since shootTimer is a local variable pointing to it, the function reference will be removed when the if block ends, and thus that function is eligible for garbage collection
Even if this was returning a thread and not a closure, once a thread dies and runs out of references it also gets collected by the GC. As long as whatever non-primitive object you have runs out of references it usually doesn’t matter the type it’ll get collected.