Skip to main content

Trash

Trash is a class that allows you to destroy an instance after a certain amount of time.

If the instance is destroyed before the time is up, the Trash object will automatically clear itself

In addition to instances, this class can be used to destroy any class object that has a Destroy method.

Migrating from Debris: Debris:AddItem(part, 8) -> Trash.new(part, 8)

Timed class destruction:

local MyClass = require(class)

local myInstance = MyClass.new()

Trash.new(myInstance, 10) -- Destroy the instance after 10 seconds

Use case:

local function partExplosion()
	for i = 1, 100 do
		local part = Instance.new("Part")
		part.Parent = Workspace

		Trash.new(part, 10) -- Destroy the part after 10 seconds

		part.Touched:Connect(function()
			part:Destroy()
			-- `Trash` will now automatically clear the Trash object
		end)
	end
end

Functions

empty

Trash.empty() → ()

Destroys all instances waiting to be destroyed.

removeFromTrash

Trash.removeFromTrash(instanceany) → ()

Stops an instance from being destroyed.

new

Trash.new(
instanceInstance,--

The instance to destroy

lifenumber?,--

The amount of time (seconds) to wait before destroying the instance. Defaults to 60 seconds.

callbackfunction?--

A callback function to call when the instance is destroyed

) → Trash

Creates a new Trash object

Destroy

Trash.Destroy(selfTrash) → ()

Destroys a Trash object

Show raw api
{
    "functions": [
        {
            "name": "empty",
            "desc": "Destroys all instances waiting to be destroyed.",
            "params": [],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 89,
                "path": "src/Trash/init.luau"
            }
        },
        {
            "name": "removeFromTrash",
            "desc": "Stops an instance from being destroyed.",
            "params": [
                {
                    "name": "instance",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 98,
                "path": "src/Trash/init.luau"
            }
        },
        {
            "name": "new",
            "desc": "Creates a new Trash object",
            "params": [
                {
                    "name": "instance",
                    "desc": "The instance to destroy",
                    "lua_type": "Instance"
                },
                {
                    "name": "life",
                    "desc": "The amount of time (seconds) to wait before destroying the instance. Defaults to 60 seconds.",
                    "lua_type": "number?"
                },
                {
                    "name": "callback",
                    "desc": "A callback function to call when the instance is destroyed",
                    "lua_type": "function?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Trash"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 116,
                "path": "src/Trash/init.luau"
            }
        },
        {
            "name": "Destroy",
            "desc": "Destroys a Trash object",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Trash"
                }
            ],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 146,
                "path": "src/Trash/init.luau"
            }
        }
    ],
    "properties": [],
    "types": [],
    "name": "Trash",
    "desc": "Trash is a class that allows you to destroy an instance after a certain amount of time.\n\nIf the instance is destroyed before the time is up, the `Trash` object will automatically clear itself\n\nIn addition to instances, this class can be used to destroy any class object that has a `Destroy` method.\n\nMigrating from Debris:\n`Debris:AddItem(part, 8)` -> `Trash.new(part, 8)`\n\nTimed class destruction:\n```lua\nlocal MyClass = require(class)\n\nlocal myInstance = MyClass.new()\n\nTrash.new(myInstance, 10) -- Destroy the instance after 10 seconds\n```\n\nUse case:\n```lua\nlocal function partExplosion()\n\tfor i = 1, 100 do\n\t\tlocal part = Instance.new(\"Part\")\n\t\tpart.Parent = Workspace\n\n\t\tTrash.new(part, 10) -- Destroy the part after 10 seconds\n\n\t\tpart.Touched:Connect(function()\n\t\t\tpart:Destroy()\n\t\t\t-- `Trash` will now automatically clear the Trash object\n\t\tend)\n\tend\nend\n```",
    "source": {
        "line": 39,
        "path": "src/Trash/init.luau"
    }
}