So if you know CollectionService, you will know that it’s a great way to organize codes. For example, making a script that gives the ability of tagged parts to kill whoever is touching it:
local CS = game:GetService(“CollectionService”)
local tagged = CS:GetTagged(“Kill”)
for i,v in pairs(tagged) do
v.Touched:Connect(function(hit)
if game.Players:GetPlayerFromCharacter(hit.Parent) then
hit.Parent.Humanoid:TakeDamage(100)
end
end)
end
But however, while I was learning it. I was thinking, why couldn’t you just use a for loop without the service? For example:
for i,v in pairs(game.Workspace:GetChildren) do
if v:IsA(“Part”) and v.Name == “KillBrick” do
v.Touched:Connect(function(hit
if game.Players:GetPlayerFromCharacter(hit.Parent) then
hit.Parent.Humanoid:TakeDamage(100)
end
end)
end
end
If possible, what’s the use of CollectionService if for loops could do the job?
Which one should I use for my game?
What’s the difference between both of this?
If you can answer all the questions, thank you for enlightening me!