Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions content/en-us/workspace/collisions.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,27 +262,27 @@ interesting but unintended gameplay, such as characters jumping on top of each o
local PhysicsService = game:GetService("PhysicsService")
local Players = game:GetService("Players")

PhysicsService:RegisterCollisionGroup("Characters")
PhysicsService:CollisionGroupSetCollidable("Characters", "Characters", false)

local function onDescendantAdded(descendant)
-- Set collision group for any part descendant
if descendant:IsA("BasePart") then
descendant.CollisionGroup = "Characters"
end
end

local function onCharacterAdded(character)
-- Process existing and new descendants for physics setup
for _, descendant in character:GetDescendants() do
onDescendantAdded(descendant)
end
character.DescendantAdded:Connect(onDescendantAdded)
local CollisionGroupName = "Characters"
PhysicsService:RegisterCollisionGroup(CollisionGroupName)
PhysicsService:CollisionGroupSetCollidable(CollisionGroupName, CollisionGroupName, false)

local function setCollisionGroup(model)
-- Apply collision group to all existing parts in the model
for _, descendant in pairs(model:GetDescendants()) do
if descendant:IsA("BasePart") then
descendant.CollisionGroup = CollisionGroupName
end
end
end

Players.PlayerAdded:Connect(function(player)
-- Detect when the player's character is added
player.CharacterAdded:Connect(onCharacterAdded)
player.CharacterAdded:Connect(function(character)
setCollisionGroup(character)
end)
-- If the player already has a character, apply the collision group immediately
if player.Character then
setCollisionGroup(player.Character)
end
end)
```

Expand Down
Loading