Help with debris system

Hi,
So I have an item spawning system that spawns items on a loop. Im using debris service that when the item’s parent is workspace I use debris:AddItem When the player clicks the item, it is parented to their character. I want to make it so if the parent of the item isn’t workspace, the debris timer cancels out, the only problem is since the item starts out as a child of workspace the timer begins and doesn’t stop even when the parent is changed. Is there anyway to cancel this out? Thanks.

1 Like

I assume you are talking about cancelling the timer after it is no longer a descendant of workspace,
Then, Debris is not designed for what you need.
From Debris :

For this reason it is the recommended method for cleaning up objects with a fixed lifetime.

I would simply reccomend instead of Debris:AddItem,

task.delay(0,function() -- replace 0 with original debris time
    if not part or not part.Parent:IsA("Model") then -- Model instead of Workspace as you said it's parented to the charachter
        part:Destroy()
    end
end)

As it will be almost identical according to the API description of Debris:

After the lifetime argument has elapsed (in seconds) the object is removed in the same manner as Instance:Destroy .

Probably should be better to use task.delay() for this.

what’s the difference exactly between these two?

task.delay is newer and takes time as it’s first argument instead of a function ( there also is task.spawn() and task.wait() which replace spawn() and wait() ).

task.delay() is just the modern (thus task.) version of delay() that uses the engine’s Task Scheduler as described in:

and takes time as it’s first argument instead of a function

instead? As far as I know, delay takes the same arguments as task.delay

I meant compared to task.spawn().

Yeah its not working I probably should have mentioned this, but I have the item in replicated storage and im using a while wait loop which inside im cloning the item and setting the parent to workspace so how would I go about doing this?

Could you please show with the script code?

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Item = ReplicatedStorage:WaitForChild("Item")
local SpawnFolder = workspace.SpawnFolder

local SpawnArray = {
	-- Array of spawn points where the item is positioned to.
}

while wait(5) do
	local NewItem = Item:Clone()
	local NewItemPosition = SpawnArray[math.random(1,10)]
	NewItem.Parent = workspace
	NewItem.Handle.Position = NewItemPosition.Position
end

The Item is a tool by the way.

Well, obviously, since the code is nowhere to be found.
EDIT: With the context, insert before the loop’s end:

task.delay(0,function() -- replace 0 with original debris time
    if not NewItem or not NewItem.Parent:IsA("Model") then
        NewItem:Destroy()
    end
end)

yeah im just not sure where to put it I tried putting it in different places but doesn’t work

Yeah that’s just not working. maybe because since its a loop its resetting the timer each time it runs?

Definetly not, as task.delay spawns a new thread.
Could you show me the script with the code inserted, And does it give you any errors?

No Errors

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Item = ReplicatedStorage:WaitForChild("Item")
local SpawnFolder = workspace.SpawnFolder

local SpawnArray = {
	-- Array of spawn points where the item is positioned to.
}

while wait(5) do
	local NewItem = Item:Clone()
	local NewItemPosition = SpawnArray[math.random(1,10)]
	NewItem.Parent = workspace
	NewItem.Handle.Position = NewItemPosition.Position
task.delay(10,function() -- replace 0 with original debris time
    if not NewItem or not NewItem.Parent:IsA("Model") then
        NewItem:Destroy()
    end
end)
end

I also have a different script inside the original item that makes it so that when a player clicks the item its parented to their character. It seems like the code isn’t even getting to the task.delay function I put a print in it and its not running. EDIT: I meant to say its not getting past the if statement, the task.delay function is running just not past the if statement.

Don’t see how the if statement could be wrong as Im parenting the item to the players character when they click it and it starts out parented to workspace so it should work EDIT: For some reason it thought workspace was a model. I don’t know why . I just parented the cloned items to the spawn parts and it works thank you.