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:


1wait(3)
2projectile:Destroy()
3

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).


1delay(3, function()
2 if projectile and projectile.Parent then
3 projectile:Destroy()
4 end
5end)
6

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.


1Debris:AddItem(projectile, 3)
2

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

1local Debris = game:GetService("Debris")
2
3local ball = Instance.new("Part")
4ball.Anchored = false
5ball.Shape = Enum.PartType.Ball
6ball.TopSurface = Enum.SurfaceType.Smooth
7ball.BottomSurface = Enum.SurfaceType.Smooth
8ball.Size = Vector3.new(1, 1, 1)
9
10local RNG = Random.new()
11local MAX_VELOCITY = 10
12
13while true do
14 local newBall = ball:Clone()
15 newBall.BrickColor = BrickColor.random()
16 newBall.CFrame = CFrame.new(0, 30, 0)
17 newBall.Velocity = Vector3.new(
18 RNG:NextNumber(-MAX_VELOCITY, MAX_VELOCITY),
19 0,
20 RNG:NextNumber(-MAX_VELOCITY, MAX_VELOCITY)
21 )
22 newBall.Parent = game.Workspace
23 Debris:AddItem(newBall, 2)
24 task.wait(0.1)
25end

Summary

Properties

Events

Methods

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

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

SetLegacyMaxItems(enabled: boolean): void  

Controls whether or not the Debris.MaxItems property should use a legacy method or not.

Properties

Events

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:


1wait(3)
2projectile:Destroy()
3

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:


1delay(3, function()
2 if projectile and projectile.Parent then
3 projectile:Destroy()
4 end
5end)
6

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.


1Debris:AddItem(projectile, 3)
2

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

No return.

Code Samples

Debris AddItem

1local Debris = game:GetService("Debris")
2
3local ball = Instance.new("Part")
4ball.Anchored = false
5ball.Shape = Enum.PartType.Ball
6ball.TopSurface = Enum.SurfaceType.Smooth
7ball.BottomSurface = Enum.SurfaceType.Smooth
8ball.Size = Vector3.new(1, 1, 1)
9
10local RNG = Random.new()
11local MAX_VELOCITY = 10
12
13while true do
14 local newBall = ball:Clone()
15 newBall.BrickColor = BrickColor.random()
16 newBall.CFrame = CFrame.new(0, 30, 0)
17 newBall.Velocity = Vector3.new(
18 RNG:NextNumber(-MAX_VELOCITY, MAX_VELOCITY),
19 0,
20 RNG:NextNumber(-MAX_VELOCITY, MAX_VELOCITY)
21 )
22 newBall.Parent = game.Workspace
23 Debris:AddItem(newBall, 2)
24 task.wait(0.1)
25end

SetLegacyMaxItems

void
Local User Security

Controls whether or not the Debris.MaxItems property should use a legacy method or not.

Parameters

enabled: boolean

Returns

void