Collision groups are driving me insane

I’ve had this issue for about two days. I have this script that creates new mechanisms locally but other players seem to be able to interact with the local part. I’m trying to disable this but I can’t seem to and I don’t know why.

I have a script on the server that creates collision groups and then I have a localscript to change all parts to be interactable with the local player but not with other players or other parts.
Server script:

local physicsService = game:GetService('PhysicsService')
local players = game:GetService('Players')
physicsService:CreateCollisionGroup('Parts')
physicsService:CreateCollisionGroup('OtherPlayers')
physicsService:CreateCollisionGroup('LocalPlayer')
physicsService:CollisionGroupSetCollidable('OtherPlayers', 'Parts', false)
physicsService:CollisionGroupSetCollidable('OtherPlayers', 'OtherPlayers', false)
physicsService:CollisionGroupSetCollidable('Parts', 'Parts', false)
physicsService:CollisionGroupSetCollidable('LocalPlayer', 'OtherPlayers', false)
physicsService:CollisionGroupSetCollidable('LocalPlayer', 'Parts', true)
physicsService:CollisionGroupSetCollidable('LocalPlayer', 'LocalPlayer', false)

players.PlayerAdded:Connect(function(player)
	local char = player.Character or player.CharacterAdded:Wait()
	local old
	for i,v in pairs(char:GetDescendants()) do
		if v:IsA('BasePart') then
			print('op',v)
			physicsService:SetPartCollisionGroup(v, 'OtherPlayers')
			v.CanCollide = false
		end
	end
	old = char.ChildAdded:Connect(function(desc)
		if desc:IsA('BasePart') then
			physicsService:SetPartCollisionGroup(desc, 'OtherPlayers')
			desc.CanCollide = false
		end
	end)
	player.CharacterAdded:Connect(function(char)
		if old then
			old:Disconnect()
		end
		for i,v in pairs(char:GetDescendants()) do
			if v:IsA('BasePart') then
				physicsService:SetPartCollisionGroup(v, 'OtherPlayers')
				v.CanCollide = false
			end
		end
		old = char.ChildAdded:Connect(function(desc)
			if desc:IsA('BasePart') then
				print('op',desc)
				physicsService:SetPartCollisionGroup(desc, 'OtherPlayers')
				desc.CanCollide = false
			end
		end)
	end)
end)

StarterPlayerScripts:

for i,v in pairs(workspace:GetDescendants()) do
	if v:IsA('BasePart') and (not players:GetPlayerFromCharacter(v:FindFirstAncestorOfClass('Model'))) then
		physicsService:SetPartCollisionGroup(v, 'Parts')
		print('parts', v)
	end
end

workspace.DescendantAdded:Connect(function(desc)
	if desc:IsA('BasePart') and (not players:GetPlayerFromCharacter(desc:FindFirstAncestorOfClass('Model'))) then
		physicsService:SetPartCollisionGroup(desc, 'Parts')
		print('parts', desc)
	end
end)

Then StarterCharacterScripts:

local old
	for i,v in pairs(script.Parent:GetDescendants()) do
		if v:IsA('BasePart') then
			physicsService:SetPartCollisionGroup(v, 'LocalPlayer')
		end
	end
	if old then
		old:Disconnect()
	end
	old = script.Parent.DescendantAdded:Connect(function()
		for i,v in pairs(script.Parent:GetDescendants()) do
			if v:IsA('BasePart') then
				physicsService:SetPartCollisionGroup(v, 'LocalPlayer')
			end
		end
	end)

But, it’s soooo unreliable, you can collide with other players half of the time, and other players can interact with your part as well.

What am I doing wrong???

Collision groups only work on the server. And they replicate to all clients, no matter if you just send the information to a single one. That collision “area” gets replicated to everyone.

So what’s happening is that even though the client cant “see” the part, the collision area on the server is still being replicated to everyone.

That’s why I made the Parts collision group so other players won’t be able to interact with any client created parts in the workspace.

I feel stupid it was bc I was adding the .PlayerAdded event after an asynchronous part

1 Like