Debris

Show Deprecated
not creatable
service

The Debris service allows the developer to schedule the removal of the object without yielding any code, through the usage of the Debris:AddItem() method.

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

As Debris is a service it must be created using the ServiceProvider:GetService() method.

Why use Debris?

Beyond creating a bit of a mess, objects that are no longer required can use up system memory and cause the game to run slower over time. For this reason it is always advised to run the Instance:Destroy() function on objects you no longer need. However in many cases an object may have a specific period of utility after which it needs to be destroyed.

Take the example of projectile that has just been thrown. It could be cleaned up using:


wait(3)
projectile:Destroy()

However there are a number of issues with this approach. Firstly, it requires yielding the code with a wait, which is not always desirable. Secondly, before the 3 seconds have elapsed the object may have already been destroyed (for example, if it reached Workspace.FallenPartsDestroyHeight).


delay(3, function()
if projectile and projectile.Parent then
projectile:Destroy()
end
end)

This solves the above issues, as it spawns a new thread to prevent the current one from yielding and checks to see if it can be destroyed. However at this point a simple command has already become quite complicated and an unnecessary thread is being created.

This is where Debris comes in, and the following code addresses all of the above issues.


Debris:AddItem(projectile, 3)

Debris does not yield the current thread, does not require a new thread and will not error if the object is already destroyed. For this reason it is the recommended method for cleaning up objects with a fixed lifetime.

Code Samples

Debris AddItem

local Debris = game:GetService("Debris")
local ball = Instance.new("Part")
ball.Anchored = false
ball.Shape = Enum.PartType.Ball
ball.TopSurface = Enum.SurfaceType.Smooth
ball.BottomSurface = Enum.SurfaceType.Smooth
ball.Size = Vector3.new(1, 1, 1)
local RNG = Random.new()
local MAX_VELOCITY = 10
while true do
local newBall = ball:Clone()
newBall.BrickColor = BrickColor.random()
newBall.CFrame = CFrame.new(0, 30, 0)
newBall.Velocity = Vector3.new(
RNG:NextNumber(-MAX_VELOCITY, MAX_VELOCITY),
0,
RNG:NextNumber(-MAX_VELOCITY, MAX_VELOCITY)
)
newBall.Parent = game.Workspace
Debris:AddItem(newBall, 2)
task.wait(0.1)
end

Summary

Methods

  • AddItem(item: Instance,lifetime: number):void

    Allows the developer to schedule the removal of the object without yielding any code.

Properties

Methods

AddItem

void

Allows the developer to schedule the removal of the object without yielding any code.

Registers a given Instance to the Debris service for removal after the specified delay. The first argument is the object being removed, and the second argument is the amount of time in seconds the Debris service will wait before removing the object. The delay argument is optional, if it is not specified, it defaults to 10 seconds. The delay argument is a number, so it accepts decimal points, such as '1.5', or '0.25'.

Why use Debris?

Beyond creating a bit of a mess, objects that are no longer required can use up system memory and cause the game to run slower over time. For this reason it is always advised to run the Instance:Destroy() function on objects you no longer need. However in many cases an object may have a specific period of utility after which it needs to be destroyed.

Take the example of projectile that has just been thrown. On first thought, it could be cleaned up using:


wait(3)
projectile:Destroy()

However there are a number of issues with this approach. Firstly, it requires yielding the code with a wait, which is not always desirable. Secondly, before the 3 seconds have elapsed the object may have already been destroyed (for example, if it reached Workspace.FallenPartsDestroyHeight). In this case, the code would error as it tries to destroy an item that has already been destroyed. One answer may be:


delay(3, function()
if projectile and projectile.Parent then
projectile:Destroy()
end
end)

This solves the above issues, as it spawns a new thread to prevent the current one from yielding and checks to see if it can be destroyed. However at this point a simple command has already become quite complicated and an unnecessary thread is being created.

This is where Debris comes in, and the following code addresses all of the above issues.


Debris:AddItem(projectile, 3)

Debris does not yield the current thread, does not require a new thread and will not error if the object is already destroyed. For this reason it is the recommended method for cleaning up objects with a fixed lifetime.

Parameters

item: Instance

The Instance to be added to Debris.

lifetime: number

The number of seconds before the Instance should be destroyed.

Default Value: 10

Returns

void

Code Samples

Debris AddItem

local Debris = game:GetService("Debris")
local ball = Instance.new("Part")
ball.Anchored = false
ball.Shape = Enum.PartType.Ball
ball.TopSurface = Enum.SurfaceType.Smooth
ball.BottomSurface = Enum.SurfaceType.Smooth
ball.Size = Vector3.new(1, 1, 1)
local RNG = Random.new()
local MAX_VELOCITY = 10
while true do
local newBall = ball:Clone()
newBall.BrickColor = BrickColor.random()
newBall.CFrame = CFrame.new(0, 30, 0)
newBall.Velocity = Vector3.new(
RNG:NextNumber(-MAX_VELOCITY, MAX_VELOCITY),
0,
RNG:NextNumber(-MAX_VELOCITY, MAX_VELOCITY)
)
newBall.Parent = game.Workspace
Debris:AddItem(newBall, 2)
task.wait(0.1)
end

Events