Player-Player Collissions cause a lot of lag in the game

Hi, I have been observing the performance of my game and it seems that it tends to lag a lot, not always but constantly.

Researching on the console, I found that the No Collision Between Players script could be the cause, sometimes the ping tends to go up to 8000ms and the No Collision script has ranges (/ s) up to 1872

Captura de Pantalla 2021-05-26 a la(s) 9.09.22

Captura de Pantalla 2021-05-26 a la(s) 9.09.54

The script I have been using is the one proposed by Roblox at Player-Player Collisions, I put it here so you have it right here. The script is in a Server Script and only 16 people enter in the game per server.

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)

I was researching in Google and in the devforum but I haven’t found anything specific about this script, has anyone come across this problem and created a different script to avoid collision between players? or, does anyone think if something inside the script is throwing this error?

Thank you very much in advance.

This is my first topic :sweat_smile:

4 Likes

Your script looks overly complicated for the task and is using more resources than needed to execute your desired task

This is the script I use for disabling players from colliding:

local Players = game:GetService("Players")
local PhysicsService = game:GetService("PhysicsService")

PhysicsService:CreateCollisionGroup("Player")
PhysicsService:CollisionGroupSetCollidable("Player", "Player", false)

function SetCollisionGroup(Part)
	PhysicsService:SetPartCollisionGroup(Part, "Player")
end

function OnPlayerAdded(Player)
	
	local function OnCharacterAdded(Character)
		for Index, Value in pairs(Character:GetDescendants()) do
			if Value:IsA("BasePart") then
				SetCollisionGroup(Value)
			end
		end
		
		local function OnDescendantAdded(Descendant)
			if Descendant:IsA("BasePart") then
				SetCollisionGroup(Descendant)
			end
		end
		
		Character.DescendantAdded:Connect(OnDescendantAdded)
	end
	
	Player.CharacterAdded:Connect(OnCharacterAdded)
	
	if Player.Character then
		OnCharacterAdded(Player.Character)
	end
	
end

for Index, Value in pairs(Players:GetChildren()) do
	OnPlayerAdded(Value)
end

Players.PlayerAdded:Connect(OnPlayerAdded)
4 Likes

Thank you very much for the help! Your script seems to work fine in Roblox Studio, I’m going to test it in the published game.

1 Like