local PhysicsService = game:GetService("PhysicsService")
local Players = game:GetService("Players")
PhysicsService:RegisterCollisionGroup("Characters")
PhysicsService:CollisionGroupSetCollidable("Characters", "Characters", false)
local function setCollisionGroup(character)
for _, part in pairs(character:GetDescendants()) do
if part:IsA("BasePart") then
part.CollisionGroup = "Characters"
end
end
end
local function onCharacterAdded(character)
setCollisionGroup(character)
-- Listen for changes to descendants
character.DescendantAdded:Connect(function(descendant)
onCharacterAdded(descendant)
end)
end
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
onCharacterAdded(character)
end)
end)
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.
When listening for descendants, you’re not setting the collision group of the new descendant if it’s a BasePart. Instead, you check its descendants and set its collision group if it’s a BasePart.
local function onCharacterAdded(character)
setCollisionGroup(character)
-- Listen for changes to descendants
character.DescendantAdded:Connect(function(descendant)
if descendant:IsA("BasePart") then
descendant.CollisionGroup = "Characters"
end
end)
end
I recommend making a script in StarterCharacterScripts instead because for some reason player.CharacterAdded won’t fire when Players.PlayerAdded fires. Any script under StarterCharacterScripts will run when a player’s character loads or respawns.
-- Server Script in StarterPlayer.StarterCharacterScripts
local PhysicsService = game:GetService("PhysicsService")
local Character = script.Parent
local collisionGroup = "Characters"
if PhysicsService:IsCollisionGroupRegistered(collisionGroup) == false then -- in case the collision group hasn't been registered yet (IMPORTANT)
PhysicsService:RegisterCollisionGroup(collisionGroup)
end
if PhysicsService:CollisionGroupsAreCollidable(collisionGroup, collisionGroup) == true then -- in case Characters is still collidable (IMPORTANT)
PhysicsService:CollisionGroupSetCollidable(collisionGroup, collisionGroup, false)
end
for _, Limb in ipairs(Character:GetDescendants()) do
if Limb:IsA("BasePart") then
Limb.CollisionGroup = collisionGroup
end
end
Character.DescendantAdded:Connect(function(Limb)
if Limb:IsA("BasePart") then
Limb.CollisionGroup = collisionGroup
end
end)