Collision Groups

Hi,

I am trying to set up Collision Groups in code but I don’t understand how to add multiple parts? can you specify a folder and all subparts? the Roblox doc’s don’t go over this?

local PhysicsService = game:GetService("PhysicsService")

local Sleepers = "Sleepers"
local Train = "Train"

-- Create two collision groups
PhysicsService:CreateCollisionGroup(Sleepers)
PhysicsService:CreateCollisionGroup(Train)

-- Add an object to each group
PhysicsService:SetPartCollisionGroup(workspace.Sleepers, Sleepers)
PhysicsService:SetPartCollisionGroup(workspace.MineCart, Train)

PhysicsService:CollisionGroupSetCollidable(Train, Sleepers, false)
1 Like

Sadly no you can’t set the collisiongroup of a folder as it takes a basepart
image

However what you can do is loop through the folder with a for loop and GetDescendants and set the collisiongroup of the baseparts in that folder.

local PhysicsService = game:GetService("PhysicsService")

local function SetCollisionGroup(instance, group)
	-- loop through and set group
	for _, v in next, instance:GetDescendants() do
		if v:IsA("BasePart") then
			PhysicsService:SetPartCollisionGroup(v, group)
		end
	end

	-- when parts are added set their group too
	instance.DescendantAdded:Connect(function(descendant)
		if descendant:IsA("BasePart") then
			PhysicsService:SetPartCollisionGroup(descendant, group)
		end
	end)
end

local Sleepers = "Sleepers"
local Train = "Train"

-- Create two collision groups
PhysicsService:CreateCollisionGroup(Sleepers)
PhysicsService:CreateCollisionGroup(Train)

-- Add an object to each group
SetCollisionGroup(workspace.Sleepers, Sleepers)
SetCollisionGroup(workspace.MineCart, Train)

PhysicsService:CollisionGroupSetCollidable(Train, Sleepers, false)

thanks that is brilliant

Julian