Player-Player collision group not working properly!

What I am trying to achieve is a system, where players can go through each other.

The issue is, the script that I’ve used, that was located on the roblox wiki randomly stopped working. To be more clear, the script doesn’t work on the main game, but on another place it works. Code:

local PhysicsService = game:GetService("PhysicsService")
local Players = game:GetService("Players")
 
local playerCollisionGroupName = "Players"
PhysicsService:CreateCollisionGroup(playerCollisionGroupName)
PhysicsService:CollisionGroupSetCollidable(playerCollisionGroupName, playerCollisionGroupName, false)
 
local previousCollisionGroups = {}
 
local function setCollisionGroup(object)
  if object:IsA("BasePart") then
    previousCollisionGroups[object] = object.CollisionGroupId
    PhysicsService:SetPartCollisionGroup(object, playerCollisionGroupName)
  end
end
 
local function setCollisionGroupRecursive(object)
  setCollisionGroup(object)
 
  for _, child in ipairs(object:GetChildren()) do
    setCollisionGroupRecursive(child)
  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)
  setCollisionGroupRecursive(character)
 
  character.DescendantAdded:Connect(setCollisionGroup)
  character.DescendantRemoving:Connect(resetCollisionGroup)
end
 
local function onPlayerAdded(player)
  player.CharacterAdded:Connect(onCharacterAdded)
end
 
Players.PlayerAdded:Connect(onPlayerAdded)
lua

Keep in mind, there is no errors whatsoever, and I added a print, one at the start, and one at the end, and both prints came through, signifying that the whole script has been run. The script should be working, as it worked on another place, even after this issue occurred, and the script worked originally in the same place, that it stopped working on.

I have tried looking on Developer Forums for help, roblox developer hub, and friends, but no fix has been found to this day.

3 Likes

If it works in one game but not another it means one of two things:

  1. You edited the script
  2. You have some other script that edits collision groups

See if you did any of those

OMG, thank you so much, I had another script that edits collison groups, looking for a fix right now, will update you if I find one. How would I modify to script to support this issue? other script:

wait (1)
local npcs=workspace.Characters

local ps=game:GetService("PhysicsService")

ps:CreateCollisionGroup("PlayersGroup")
ps:CreateCollisionGroup("NpcsGroup")

ps:CollisionGroupSetCollidable("PlayersGroup", "NpcsGroup", false)

for _,npc in pairs(npcs:GetChildren()) do
     for _,v in pairs(npc:GetDescendants()) do
          if v:IsA("BasePart") then
               ps:SetPartCollisionGroup(v,"NpcsGroup")
          end
     end
end

game.Players.PlayerAdded:Connect(function(plr)
     plr.CharacterAdded:Connect(function(char)
          wait()
          for _,v in pairs(char:GetChildren()) do
               if v:IsA("BasePart") then
                    ps:SetPartCollisionGroup(v,"PlayersGroup")
               end
          end
     end)
end)
lua

I edited my script to allow players go to through each other. You can delete the first one you had since it’s no longer needed and this does both.

wait (1)
local npcs=workspace.Characters

local ps=game:GetService("PhysicsService")

ps:CreateCollisionGroup("PlayersGroup")
ps:CreateCollisionGroup("NpcsGroup")

ps:CollisionGroupSetCollidable("PlayersGroup", "NpcsGroup", false)
ps:CollisionGroupSetCollidable("PlayersGroup", "PlayersGroup", false)

for _,npc in pairs(npcs:GetChildren()) do
     for _,v in pairs(npc:GetDescendants()) do
          if v:IsA("BasePart") then
               ps:SetPartCollisionGroup(v,"NpcsGroup")
          end
     end
end

game.Players.PlayerAdded:Connect(function(plr)
     plr.CharacterAdded:Connect(function(char)
          wait()
          for _,v in pairs(char:GetChildren()) do
               if v:IsA("BasePart") then
                    ps:SetPartCollisionGroup(v,"PlayersGroup")
               end
          end
     end)
end)
6 Likes