Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to use the Debris service?

I've heard about it, and I wonder what it is, but the wiki isn't very informative and also, how do you use it?

1 answer

✓ Accepted answer

##Overview

The Debris service is used to remove items after a certain amount of time after they were added it to it. This is useful for handling things like debris, where you only want parts to appear for a short period of time before they disappears (hence where it got it's name).

There is only a single method in the Debris service which is AddItem. This method takes two arguments, the instance you want to add and how long it should take (in seconds) before that item is destroyed.

##Example Usage

This script contains a function that will create a group of random parts randomly offset from a given position. They are added to the Debris service and destroyed after a random amount of time between 1 and 6 seconds.

local Debris = game:GetService("Debris")

function createDebris(position, amount)
	for i = 1, amount do
		local debris = Instance.new("Part", game.Workspace)
		local offset = Vector3.new(math.random(-5, 5), math.random(-5, 5), math.random(-5, 5))
		debris.Position = position + offset
		debris.Size = Vector3.new(1, 1, 1)
		debris.BrickColor = BrickColor.Random()
		Debris:AddItem(debris, 1 + math.random()*5)
	end
end

createDebris(Vector3.new(0, 10, 0), 10)

##Notes ####(taken from the wiki) The Debris service will remove the object in the same manner the Destroy method does.

If AddItem is called from a LocalScript and the player who made the call leaves the game before the item is removed, it will not be removed.

This discussion is preserved as it was. New answers, comments, and votes are closed.