Is this the correct way to use CollectionService?

I am trying to figure out CollectionService and how i could make it work for multiple parts and i came up with this.
Is there anything to improve? Or is this the correct way to use it?

local __CS = game:GetService("CollectionService")
local __CONNECTIONS = {}

function StartProm(__INST:BasePart?)
	__CONNECTIONS[__INST] = {}
	
	table.insert(__CONNECTIONS[__INST], __INST:FindFirstChildOfClass("ProximityPrompt").TriggerEnded:Connect(function(__PLR)
		print(__PLR.Name)
	end))
end
local __VENDING = __CS:GetTagged("__VNDMCHN")
for count=1, #__VENDING do
	local __INT = __VENDING[count]
	StartProm(__INT)
end
-- TAG HANDLER APPLIER

for _,v in ipairs(workspace:GetChildren()) do
	if v.Name == "__VENDMACHINE" then
		__CS:AddTag(v, "__VNDMCHN")
	end
end

Why not just use have them start with the tag?


You might want to also use GetInstanceAddedSignal to correctly handle new things with the tag getting added after the initial loop is done.

Is there a way? I couldn’t find a way in properties.

1 Like

You can install the Tag Editor Plugin for this task.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.