Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make parts have a lifespan?

Asked by 6 years ago
Edited 6 years ago

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

Log in to vote
3
Answered by
yHasteeD 1819 Moderation Voter
6 years ago
Edited 6 years ago

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!

0
You provided a duplicate example of each. Did you use this just to extend the answer? User#19524 175 — 6y
0
I wanted to demonstrate several ways how to use yHasteeD 1819 — 6y
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! xEmmalyx 285 — 6y
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. User#19524 175 — 6y
View all comments (4 more)
0
I used variables to make it easier for the person to understand and also to be completely editable. yHasteeD 1819 — 6y
0
but they can do it themselves User#19524 175 — 6y
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. DinozCreates 1070 — 6y
0
Agree 1TheNoobestNoob 717 — 6y
Ad

Answer this question