So I have this problem, I want players not to collide with each other. I copied the code I found here,
but I adjusted it a bit, I split it, and the main part is in a modulescript because I have 2 scripts that make use of it, the first one is the second part of the script I split (sets them to a collision group that doesn’t collide with each other), and the second one is setting the players’ collision group to another one and then back to the original one, from time to time, for locked doors feature.
Obviously, I only want the previous collision group saved when the first script runs, and not when the second one runs, because then it will set the object’s collision group to a wrong one, and since the first script runs first, and only once, I added another table of values, which checks if the collision group has already been set. So now, only the first script sets the previous collision group, and the second one doesn’t.
I turned it into this
local PlayerCollisionGroupsManager = {}
local PhysicsService = game:GetService("PhysicsService")
local previousCollisionGroups = {}
local PCGAlreadySet = {} --previous collision group should only be set the first time the player's collisionGroup is changed. This table will hold values (true or false) that will then be used to check if previous collision group has been already set for an object, so it ensures that an object only had
function PlayerCollisionGroupsManager:setCollisionGroup(object, collisionGroupName)
if object:IsA("BasePart") then
if not PCGAlreadySet[object] then
previousCollisionGroups[object] = object.CollisionGroupId
PCGAlreadySet[object] = true
end
PhysicsService:SetPartCollisionGroup(object, collisionGroupName)
end
end
function PlayerCollisionGroupsManager:setCollisionGroupRecursive(object, collisionGroupName)
self:setCollisionGroup(object, collisionGroupName)
for _, child in ipairs(object:GetChildren()) do
self:setCollisionGroupRecursive(child, collisionGroupName)
end
end
function PlayerCollisionGroupsManager: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
PCGAlreadySet[object] = nil
end
return PlayerCollisionGroupsManager
If you understood what I meant, and you most likely didn’t, because even I can’t understand what I just said, but if you did, is it the best way to do this?
Now that I think about it, I realized there could be a bug if I add a basepart to the player’s children when the player’s collision group has been changed by the second script. The basepart will get the collision group of the first script, since it runs when a descendent is added to the player, and the second script doesn’t.
I think I could solve these problem by having the PlayerCollisionGroupsManager script (The one I showed) require the other two scripts, and then perform checks there, instead of these two scripts requirng the PlayerCollisionGroupsManager.