Help understanding modules

CollectionService is useful for ensuring only one script exists when multiple models or parts that do the same thing need a script to work (such as doors, kill bricks, lights, etc.).

However, there is no difference performance-wise if you do CollectionService instead of iterating over all the instances in a folder or model with a for loop to connect to each instances’ .Touched events. CollectionService just serves as an easier way to access instances with the tag globally.

CollectionService Documentation
CollectionService in a nutshell

Here’s an example of how CollectionService works for making parts that damage the player by 15 points on touch. More complicated scripts can be made, but this is a simple one that can be recreated easily.

local CollectionService = game:GetService("CollectionService")
local KillBrickTag = "KillBrick"

for i, v in CollectionService:GetTagged(KillBrickTag) do
	v.Touched:Connect(function(hitPart)
		if hitPart.Parent:FindFirstChild("Humanoid") then
			hitPart.Parent.Humanoid:TakeDamage(15)
		end
	end)
end
1 Like