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

How can I destroy parts that have a tag (CollectionService)?

Asked by 3 years ago

I am pretty new to using CollectionService, I already got a tag for the parts, I just wanna know, how would I be able to destroy those parts with the tag? I can't figure it out. Any help is appreciated

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

The CollectionService:GetTagged() function allows you to retrieve all the instances with a certain tag. It returns a table, through which you can iterate through (usually by a for loop) and do what you want to each instance.

For example:

local CollectionService = game:GetService("CollectionService")
local tagged = CollectionService:GetTagged("tag")

for index, value in pairs(tagged) do
    print(value.Name)
end

Or in your case, you replace line five with value:Destroy().

0
Read the documentation for CollectionService at: https://developer.roblox.com/en-us/api-reference/function/CollectionService/ radiant_Light203 1166 — 3y
0
is it possible for me to add it in a while loop? also everytime i try to add wait(seconds) to it, it wont work Sold50ChildrenToday 9 — 3y
0
You'll have to further describe your use case. Although you can, I don't see why as it would be very round-about; also for loops are very different from while loops and wait() cannot come in the declaring line, only inside the scope. radiant_Light203 1166 — 3y
0
For more information on loops, read here: https://developer.roblox.com/en-us/articles/Loops radiant_Light203 1166 — 3y
Ad

Answer this question