Reference API Roblox

Engine API

Website

Related

Reference API Roblox

Debris

Allows scheduling the guaranteed destruction of an object without yielding.
.

This class is not creatable. Instances of this class cannot be created with Instance.new.
This class is a service. It is a singleton that may be acquired with GetService.
Tags: [NotCreatable, Service]

Member index 4

HistoryMember
553MaxItems: int
573AddItem(item: Instance, lifetime: double = 10): null
573SetLegacyMaxItems(enabled: bool): null
573addItem(item: Instance, lifetime: double = 10): null
inherited from Instance
553Archivable: bool
635Capabilities: SecurityCapabilities
553Name: string
553Parent: Instance
635Sandboxed: bool
616UniqueId: UniqueId
576AddTag(tag: string): null
573ClearAllChildren(): null
462Clone(): Instance
573Destroy(): null
486FindFirstAncestor(name: string): Instance
486FindFirstAncestorOfClass(className: string): Instance
486FindFirstAncestorWhichIsA(className: string): Instance
486FindFirstChild(name: string, recursive: bool = false): Instance
486FindFirstChildOfClass(className: string): Instance
486FindFirstChildWhichIsA(className: string, recursive: bool = false): Instance
486FindFirstDescendant(name: string): Instance
563GetActor(): Actor
486GetAttribute(attribute: string): Variant
462GetAttributeChangedSignal(attribute: string): RBXScriptSignal
631GetAttributes(): Dictionary
648GetChildren(): Instances
462GetDebugId(scopeLength: int = 4): string
486GetDescendants(): Array
486GetFullName(): string
641GetStyled(name: string): Variant
576GetTags(): Array
576HasTag(tag: string): bool
486IsAncestorOf(descendant: Instance): bool
486IsDescendantOf(ancestor: Instance): bool
580IsPropertyModified(name: string): bool
573Remove(): null
576RemoveTag(tag: string): null
580ResetPropertyToDefault(name: string): null
573SetAttribute(attribute: string, value: Variant): null
462WaitForChild(childName: string, timeOut: double): Instance
648children(): Instances
553clone(): Instance
573destroy(): null
553findFirstChild(name: string, recursive: bool = false): Instance
648getChildren(): Instances
553isDescendantOf(ancestor: Instance): bool
573remove(): null
462AncestryChanged(child: Instance, parent: Instance)
462AttributeChanged(attribute: string)
462ChildAdded(child: Instance)
462ChildRemoved(child: Instance)
462DescendantAdded(descendant: Instance)
462DescendantRemoving(descendant: Instance)
500Destroying()
553childAdded(child: Instance)
inherited from Object
647ClassName: string
647className: string
647GetPropertyChangedSignal(property: string): RBXScriptSignal
647IsA(className: string): bool
650isA(className: string): bool
647Changed(property: string)

Description

The Debris service allows scheduling guaranteed destruction of an object without yielding.

Advantages

Besides creating a bit of a mess, objects that are no longer required can use up system memory and cause an experience to run slower over time. For this reason, it's always advised to call Instance:Destroy() on objects you no longer need. In some cases, however, an object may have a specific period of utility before it can be destroyed.

Consider a wall being smashed into individual bricks. If you want a brick to linger for 3 seconds before being destroyed, you can use the following code:

1
2
task.wait(3)
brick:Destroy()

However, waiting causes the thread to yield which may be undesired. To avoid yielding, a callback function can be scheduled to run on a new thread after 3 seconds:

1
2
3
task.delay(3, function()
	brick:Destroy()
end)

Or in one line:

1
task.delay(3, brick.Destroy, brick)

While this now avoids yielding, it has a potential drawback in that the scheduled callback will never run if the script is disabled or destroyed before the callback runs.

This is where Debris has a specific advantage, as it does not yield the current thread and runs outside the context of the script, guaranteeing the instance is eventually destroyed even if the script is disabled or destroyed. The following code does not yield and guarantees the instance will be destroyed:

1
Debris:AddItem(brick, 3)

Note that Debris has a hardcoded maximum of 1,000 objects, so if more than 1,000 items are added, the oldest debris will be destroyed instantly to make room for new debris.

History 14

Members 4

AddItem

Parameters (2)Default
itemInstance
lifetimedouble10
Returns (1)
null

Schedules a given Instance for destruction within the specified lifetime. After the lifetime argument has elapsed, the object is destroyed in the same manner as Instance:Destroy(). Note that the lifetime argument is optional and defaults to 10 seconds.

Note that Debris has a hardcoded maximum of 1,000 objects, so if more than 1,000 items are added, the oldest debris will be destroyed instantly to make room for new debris. This means you should treat the lifetime parameter as a maximum lifetime, not an exact lifetime.

History 3

MaxItems

TypeDefault
int

The maximum number of items that can be assigned to the Debris service at one time.

If this number is exceeded, objects are automatically destroyed in order from oldest to newest until the amount is less than or equal to MaxItems.

This property is currently restricted and will error if set. The value is hardcoded to 1,000 items.

This property is deprecated. It exists only for backward compatibility, and should not be used for new work.

History 4

Tags: [Deprecated]

SetLegacyMaxItems

Parameters (1)
enabledbool
Returns (1)
null

History 3

addItem

Parameters (2)Default
itemInstance
lifetimedouble10
Returns (1)
null
This function is deprecated. It exists only for backward compatibility, and should not be used for new work.

History 3

Tags: [Deprecated]

Settings