local function z(z)if z:IsA("\066\097\115\101\080\097\114\116")then previousCollisionGroups[z]=z.CollisionGroupId PhysicsService:SetPartCollisionGroup(z,playerCollisionGroupName)end end local function j(j)z(j)for j,I in ipairs(j:GetDescendants())do z(I)end end local function I(z)local j=previousCollisionGroups[z]if not j then return end local I=PhysicsService:GetCollisionGroupName(j)if not I then return end PhysicsService:SetPartCollisionGroup(z,I)previousCollisionGroups[z]=nil end local function Z(Z)j(Z)Z.DescendantAdded:Connect(z)Z.DescendantRemoving:Connect(I)end local function O(z)z.CharacterAdded:Connect(Z)end
-- 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
