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