Simple garbage module. Made this a while back.
Use this for your game, or as a learning resource
local garbage = {}
local function cleanup(t, deep)
for i, v in next, t do
local foundType = typeof(v)
if foundType == "function" then
v()
t[i] = nil
elseif foundType == "Instance" then
v:Destroy()
elseif foundType == "RBXScriptConnection" then
v:Disconnect()
elseif foundType == "table" and deep then
cleanup(v, deep)
else
t[i] = nil
end
end
end
function garbage.new()
local VirtualBin = {}
local Methods = {}
function Methods:trash(deep)
task.spawn(cleanup, VirtualBin, deep)
end
return setmetatable(Methods, {__index = VirtualBin, __newindex = VirtualBin})
end
return garbage
It’s an insanely simple module. Usage below
local Trash = Garbage.new()
Trash.SomeInstance = Instance.new("BoolValue")
--// The functions are called upon :trash()
Trash.thisIsRanOnTrash = function() end
Trash.onTrash = function() end
--// Only cleaned if :trash(true) is called.
Trash.Table = {Instance.new("Part"), Instance.new("Model")}
Trash:trash()
--// The optional parameter "deep" allows it to collect garbage inside of tables stored within the instance.
--// Calling this method by itself would leave `Trash.Table` instances.
Trash:trash(true)
--// Would deep clean the entire table.
The module might be a bit iffy, but it does have it’s usages. Just wanted to share this with everyone
I might’ve missed some tiny optimizations