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

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

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

✓ Accepted answer

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().

4 comments

0
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

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.

0

For more information on loops, read here: https://developer.roblox.com/en-us/articles/Loops

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