How do i use collection service?

I heard colletion service can be used to decrease the load of the server, and instead apply one script into parts with the collection tag, im trying to make a script whereas each of these parts (obstacles) when touched will explode the player on impact and it doesnt seem to work, does anyone know why this is happening?
Screenshot 2024-10-24 170809

2 Likes

You’re using CollectionService correctly but the syntax isn’t written correctly.

That’s why some words are underlined red.

Try this:

local CS = game:GetService("CollectionService")

for i, model in pairs(CS:GetTagged("Obstacle")) do
    model.Touched:Connect(function(hit)
        if hit.Parent:FindFirstChild("Humanoid") then
            local Bomb = Instance.new("Explosion")
            Bomb.BlastPressure = 100000
            Bomb.BlastRadius = 10
            Bomb.Position = model.Position
            Bomb.Parent = model
        end
    end)
end
1 Like