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

How do I make parts have a lifespan?

I’m making a game where blocks rain down on you and then despawn after 5 seconds. How do I give the blocks a lifespan?

1 answer

✓ Accepted answer

From what I understand, do you want to make blocks spawn and wait a seconds then remove

If so, I have the solution and how you use it:

Wiki page: Roblox Wiki Page - Debris

You can use Debris.

Example:

game:GetService("Debris"):AddItem(script.Parent,3) -- Remove script.Parent in 3 seconds.

Or you can use delay:

delay(3, function()
	script.Parent:Destroy()
end)

Or wait:

wait(3)
script.Parent:Destroy()

The debris in my opinion is the most efficient.

For you script you can use this:

local Part = script.Parent -- Find part to remove
local DelayTime = 5

game:GetService("Debris"):AddItem(Part,DelayTime)

OR

local Part = script.Parent -- Find part to remove
local DelayTime = 5

delay(DelayTime, function()
	Part:Destroy()
end)

OR

local Part = script.Parent -- Find part to remove
local DelayTime = 5

wait(DelayTime)
Part:Destroy()

The best is the Debris.

Any errors? tell-me on comments.

Hope it helped!

8 comments

0

You provided a duplicate example of each. Did you use this just to extend the answer?

0

I wanted to demonstrate several ways how to use

0

I completely understand, it is respectable that you put more of your time into the answer to give more examples to make it easier to understand the usability!

0

But it's obvious you can use variables. You used that only to extend the answer. They're exactly the same. It would be like me answering a question on how to print 7. I would just say print(7), not print(3 + 4) or whatever.

0

I used variables to make it easier for the person to understand and also to be completely editable.

0

but they can do it themselves

0

If he doesnt know how to destroy a block likely he doesnt have a good grasp on variables either. I think Hastee explained it perfectly.

0

Agree

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