Skip to content

Instantly share code, notes, and snippets.

@RuizuKun-Dev
Created January 26, 2021 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RuizuKun-Dev/4c55f6d4e8fafc566caa43ad5c2c5b94 to your computer and use it in GitHub Desktop.
Save RuizuKun-Dev/4c55f6d4e8fafc566caa43ad5c2c5b94 to your computer and use it in GitHub Desktop.

Debris2

an extension to Roblox Debris Service

  • You can use this to:
    • Disconnect RBXScriptConnections
    • Destroy Instances
    • Remove tables with a .Destroy method
  • With Added functionality of:
    • Adding multiple items at once
    • Canceling Debris
    • Overriding lifeTime
    • Setting Debris.Destroyed callback

-- Structure

Debris2 = {
  ["Instances'] = {
    [Debris: instance or table or RBXScriptConnection] = {
     ["lifeTime"] = lifeTime: number,
      removalTime = tick() + lifeTime: number,
      Destroyed = nil, -- Destroyed: callback Function | nil by default
      Cancel = function -- remove references and disconnect Hearbeat
      ["Instance"] = item: (Instance or table or RBXScriptConnection),
    },
  },
}

-- Methods

  • AddItem (item: Instance or table or RBXScriptConnection, lifeTime: number?) -> Debris
  • AddItems (arrayOfItems: {Instance, table, RBXScriptConnection}, lifeTime: number?) -> void
  • GetAllDebris () -> Debris2.Instances
  • GetDebris (item: Instance or table or RBXScriptConnection) -> Debris2.Instances.Debris

-- Example Code

local Debris2 = require(game.ServerScriptService.Debris2)

local parts = {}

for i = 1, 5 do
	local part = Instance.new("Part")
	part.Name = i
	part.Anchored = true
	part.Parent = workspace
	local debris = Debris2:AddItem(part,3)
	function debris.Destroyed()
		print("Destroyed",part)
	end
	if i == 1 then
		debris.Cancel()
	end
	table.insert(parts,part)
end

Debris2:AddItem({},3).Destroyed = function()
	print("DESTROYED")
end
Debris2:AddItem(parts[2].AncestryChanged:Connect(function(...) print(...) end),3).Cancel()

Debris2:AddItems(parts,4)

Debris2:getDebris(parts[1]).Cancel()

wait(5)

print(#Debris2:getAllDebris())

-- Motivation

As you already know Debris doesn’t have a lot of functionality, in-fact there’s only one method you can use :AddItem And it could be quite limiting

--[[
@game/ReplicatedStorage/Debris2
Debris2 = {
Instances {
[Debris: instance or table or RBXScriptConnection] = {
lifeTime = lifeTime: number,
removalTime = tick() + lifeTime: number,
Destroyed = nil, -- Destroyed: callback Function | nil by default
Cancel = function -- remove references and disconnect Hearbeat
Instance = item: (Instance or table or RBXScriptConnection),
},
},
}
-- Methods
AddItem (item: Instance or table or RBXScriptConnection, lifeTime: number?) -> Debris
AddItems (arrayOfItems: {Instance, table, RBXScriptConnection}, lifeTime: number?) -> void
GetAllDebris () -> Debris2.Instances
GetDebris (item: Instance or table or RBXScriptConnection) -> Debris2.Instances.Debris
--]]
local Debris2 = {
Instances = {}
}
--| SERVICES:
local RunS = game:GetService("RunService")
local Hearbeat = RunS.Heartbeat
--| MODULES:
--| VARIABLES:
local Instances = Debris2.Instances
--| TABLES:
local Connections = {}
local ValidTypes = {
["Instance"] = "Destroy",
["table"] = true,
["RBXScriptConnection"] = "Disconnect",
}
local METHODS = { -- add any Custom Destroy/Remove/Clear/CleanUp methods here
"Destroy",
"Disconnect",
"destroy",
"disconnect",
}
--| META TABLES:
--| FUNCTIONS:
local function removeItem(typeOf, object)
if typeOf == "Instance" then
pcall(object.Destroy, object)
elseif typeOf == "RBXScriptConnection" then
pcall(object.Disconnect, object)
else
for _,v in ipairs(METHODS) do -- _, v: method name
if object[v] then
pcall(object[v], v)
break
end
end
end
end
local function addDebris(object, lifeTime)
local typeOf = typeof(object)
assert(ValidTypes[typeof(object)])
assert(typeof(lifeTime) == "number")
if (not Instances[object]) then
table.insert(Instances, object)
end
Instances[object] = {
["lifeTime"] = lifeTime,
removalTime = tick() + lifeTime,
-- Destroyed = nil, -- Destroyed: callback Function
Cancel = function() -- remove references and disconnect Hearbeat
Connections[object]:Disconnect()
table.remove(Instances,table.find(Instances, object))
Instances[object] = nil
end,
["Instance"] = object,
}
local debris = Instances[object]
Connections[object] = Heartbeat:Connect(function()
if debris and tick() >= debris.removalTime then
if debris.Destroyed then
debris.Destroyed()
end
debris.Cancel()
removeItem(typeOf,object)
end
debris = Instances[object]
end)
return Instances[object]
end
--| METHODS:
function Debris2:AddItem(item, lifeTime) -- item: (Instance, table, RBXScriptConnection), lifeTime: number
return addDebris(item, lifeTime)
end
function Debris2:AddItems(arrayOfItems, lifeTime) -- arrayOfItems: (Instance, table, RBXScriptConnection), lifeTime: number
for _,item in ipairs(arrayOfItems) do
addDebris(item, lifeTime)
end
end
function Debris2:GetAllDebris()
return Instances
end
Debris2.getAllDebris = Debris2.GetAllDebris
function Debris2:GetDebris(item)
return Instances[item]
end
Debris2.getDebris = Debris2.GetDebris
--| SCRIPTS:
-- return:
return Debris2
MIT License
Copyright (c) 2020 RuizuKun_Dev
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@gokuwantrobux
Copy link

Debris2 = {
["Instances'] = {
[Debris: instance or table or RBXScriptConnection] = {
["lifeTime"] = lifeTime: number,
removalTime = tick() + lifeTime: number,
Destroyed = nil, -- Destroyed: callback Function | nil by default
Cancel = function -- remove references and disconnect Hearbeat
["Instance"] = item: (Instance or table or RBXScriptConnection),
},
},
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment