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

What is addItem useful for?

Asked by 9 years ago

I see scripts that do things like this:

wait(2)
t = Instance.new("Part", Workspace)
wait(1)
game:GetService("Debris"):AddItem(t, 1)

And i just think why?, i mean you can use t:Remove() and will do the same thing, no?

3 answers

Log in to vote
1
Answered by 9 years ago

Using the Debris service, you can have an object automatically clean itself up after a set amount of time. It will also keep the objects from getting out of hand. Once the service is run, you can find its object on the bottom of the list in the Explorer pane in the Studio. Its parent is automatically game, and that cannot be changed.


The [Add Item]http://wiki.roblox.com/index.php?title=AddItem_(Method)) function is used with the Debris service. When you add an item it gets added to the list of what the game considers to be clutter on the map. After the lifetime limit has expired, the item will be removed. Or, if there are too many clutter items around and you hit the MaxItems limit, the oldest items will be removed until the number of clutter objects is back under the limit.


Just like BlueTaslem said There's no real reason to use the Debris service if you dont want to. It just a Delay.

Ad
Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

The Debris service isn't used much nowadays.


Without the second parameter to AddItem, Debris stick around for a longer time. Once you have more than a certain number (MaxItems) it starts cleaning them up.

With the second parameter, yes, it pretty much is just a delay.

Although this is significantly older than the addition of delay to ROBLOX; it would significantly reduce the boilerplate then to use this function.


There's no real reason to use the Debris service if you dont want to.

Log in to vote
0
Answered by
Nymint 85
9 years ago

In my case, I think

local Part=Workspace:WaitForChild("Part")
local Debris=Game:GetService("Debris")
Debris:AddItem(Part,5) --First Parameter as the object that is going to be removed
--Second Parameter as the amount of time you'd like to wait to later remove it.

Is really useful, because it removes a certain object without having to yield(stop) the script, wait(5) would be different, because it yields the script unless you're using coroutines for multi-threading.

Like BlueTaslem said, it's pretty much the same as delay(), but I'd prefer Debris:AddItem(Part,5)

Useful uses like this one:

local Debris=Game:GetService("Debris")
local LifeTime=6 local Radius=20
while wait(.25) do
local p=Instance.new("Part",Workspace)
Debris:AddItem(p,LifeTime)
p.Position=Vector3.new(0,50,0)
+Vector3.new(math.random(-Radius,Radius),math.random(-Radius,Radius),math.random(-Radius,Radius))
end

Answer this question