I was trying to test Tags and Collection Service. I have set-up the Tag using the Instance Tagging, and then added a block (say, Block1) to the tag.
On ServerScriptService, I have created a script with the following code:
local CollectionService = game:GetService("CollectionService")
local CollectionName = "PoppingBlocks" --The name of the Tag
local function onTouch()
print("Touched object")
end
for _, object in pairs(CollectionService:GetTagged(CollectionName)) do
object.Touched:Connect(onTouch)
end
Expected: When I touch the object, console must print "Touched object"
Touching the object does not do anything. Furthermore, how do I pass more parameters on Touched event?
local CollectionService = game:GetService("CollectionService")
local CollectionName = "PoppingBlocks" --The name of the Tag
local function onTouch(object)
print("Touched object " .. object.Name)
end
for _, object in pairs(CollectionService:GetTagged(CollectionName)) do
object.Touched:Connect(onTouch(object)) --???
end
???: Doesn't the Touch event send 'hit' or 'otherpart' as a parameter? What would the 'object' parameter in the Connect() function be?
Also, how do I destroy the block? My original method was applying this script to the block manually, and then calling script.Parent
local this = script.Parent
local function onTouch()
print("Touched object" .. this.Name .. ")
this:Destroy()
end
this.Touched:Connect(onTouch)
This method simply worked. However, for obvious reasons, it was very tedious. I found myself editing one script, and copy-pasting it to every block I need.

1 comment
Your assurance of "should worked" helped me a lot and forced me to try every other possible solution. I just had to call the loop again to update new objects in |CollectionService:GetTagged(CollectionName)|. Thank you!