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()
coolingDown = false
coolingDown = true
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
Thanks, I forgot about the garbage collection in Roblox (and Lua, if I’m not mistaken) for a second.
^ How-ever a tick() debounce would seem more practical imo.
It would, and as of now I’ve scrapped the cooldown entirely, but I also wanted to know if this would cause memory leaks, for the future.
That would be garbage collection in Lua, yes.
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.