-- Local Functions

local function setCollisionGroupForPart(part)
	if part:IsA("BasePart") then
		previousCollisionGroups[part] = part.CollisionGroupId
		PhysicsService:SetPartCollisionGroup(part, playerCollisionGroupName)
	end
end
 
local function setCollisionGroupForObject(object)
	setCollisionGroupForPart(object)
	for _, object in ipairs(object:GetDescendants()) do
		setCollisionGroupForPart(object)
	end
end
 
local function resetCollisionGroup(object)
	local previousCollisionGroupId = previousCollisionGroups[object]
	if not previousCollisionGroupId then return end 
 
	local previousCollisionGroupName = PhysicsService:GetCollisionGroupName(previousCollisionGroupId)
	if not previousCollisionGroupName then return end
 
	PhysicsService:SetPartCollisionGroup(object, previousCollisionGroupName)
	previousCollisionGroups[object] = nil
end
 
local function onCharacterAdded(character)
	setCollisionGroupForObject(character)
 
	character.DescendantAdded:Connect(setCollisionGroupForPart)
	character.DescendantRemoving:Connect(resetCollisionGroup)
end
 
local function onPlayerAdded(player)
	player.CharacterAdded:Connect(onCharacterAdded)
end

