I've come across ROBLOX's Destroy Service Module is this better then using debris?

This is the source code and what is the difference between using this and debris?

local destroyService = {}

local destroyQueue = {}

function destroyService:AddItem(theobject, delay)
	local now = os.time()
	local destroyObject = {object = theobject, destroyTime = delay + now}
	for i, storedObject in pairs(destroyQueue) do
		if destroyQueue[i].destroyTime > destroyObject.destroyTime then
			table.insert(destroyQueue, i, destroyObject)
			return true
		end
	end
	table.insert(destroyQueue, destroyObject)
	return true
end

local updateThread = coroutine.create(function()
	while true do 
		local now = os.time()
		for _, storedObject in pairs(destroyQueue) do
			if now >= storedObject.destroyTime then
				table.remove(destroyQueue, 1)
				if storedObject.object then
					storedObject.object:Destroy()
				end
			elseif now >= storedObject.destroyTime - 1 then
				
				if storedObject.object and storedObject.object:IsA("Part") then
					local trans = storedObject.object.Transparency + 1/30
					storedObject.object.Transparency = trans
				end
			else 
				break
			end
		end
		wait()
	end
end)

coroutine.resume(updateThread)

return destroyService
2 Likes

Debris is built into the engine since its a service and I think it would be better to use Debris over another module.

But they seem to do the same thing, its just more hassle with more modules

1 Like

Can’t you literally just use the :Destroy() method? Anyway, you basically answered your question. Possibly this would belong in #resources:community-resources?

Debris allows for timed destruction

Debris:AddItem(part, 10) -- will wait 10 seconds to destroy

Mhm, but you can add a task.wait(10) before the :Destroy() function…

Yeah I was wondering if it’s anymore performant, I was just looking into ROBLOX open sourced models and came across it and wonder why they didn’t just use debris?

It came from Drooling Zombie - Roblox

Its a better alternative since it doesn’t yield the script.

Its basically like doing task.delay(10, part.Destroy, part) really

Fair enough, just the Destroy function is easier and more simple than writing 50+ lines of code to basically do the same thing.

DebrisService doesn’t error. If the object is already destroyed calling Destroy() on it will error while calling AddItem() will not error. And technically speaking, less code is not better code.

task.wait is a yielding function, while AddItem is not (yielding basically “stops” code from running until the wait is done).

So you could add a sanity check, to check if is there, and if it isn’t, then you return it as nil.

Debris also allows for easier usage of clearing up a lot of instances at once, and as @VegetationBush said it doesn’t have the problem of errors.

for i,v in pairs(Ragdolls:GetChildren()) do
  Debris:AddItem(v, 10)
end

instead of

for i, v in pairs(Ragdolls:GetChildren()) do
  task.delay(10, v.Destroy, v) -- can error and is more less performant
end

Didn’t you say that you would rather call destroy instead of running 50 lines of code? Why not write one line of code:

Debris.AddItem(Instance, 0)

instead of 3:

if Instance then
    Instance:Destroy()
end

It’s the same logic applied with your reasoning.

Just because there are more lines doesn’t mean it’s bad. Everything has its usecase.

So you’re saying that Debris is better than Destroy? Fair enough!

No…

He said its better at its job, :Destroy ~= :AddItem

Its just better at scheduling destruction.

1 Like
local item = Instance.new("Part", workspace)
spawn(function() task.wait(10) item:Destroy() end)

This fixes the yielding problem. The only issue is the erroring and the less code.
Having a required mainmodule that makes a folder called “Debris” and then has a function that parents to that folder after a set interval.

--MainModule
local debrisFolder = Instance.new("Folder")
local module = {}
function module.Add(part,int)
     spawn(function() 
         task.wait(int) 
         part.Parent = debrisFolder  
     end)
end
return module
--Script
local part  = workspace.Baseplate
local debrisService2 = require({MODULE ID HERE})
debrisService2.Add(part, 10)

With this code, it does the same things that you said about debris service. It won’t error if you do Add() twice

You could do the same in a very little bit of code

pcall(task.delay, 10, part.Destroy, part)

but i prefer Debris:AddItem(part, 10)

Less ~= better
The way I posted prevents code from erroring when calling Add() twice by accident

How does it stop it from erroring if called twice?

Read the code and you would know. It doesn’t call :Destroy()

But… thats the whole point of Debris service…