Skip to content

Instantly share code, notes, and snippets.

@MagmaBurnsV
Created June 14, 2023 20:31
Show Gist options
  • Save MagmaBurnsV/f93a808d45815d6a580890d02783774f to your computer and use it in GitHub Desktop.
Save MagmaBurnsV/f93a808d45815d6a580890d02783774f to your computer and use it in GitHub Desktop.
--!strict
local Players = game:GetService("Players")
local PhysicsService = game:GetService("PhysicsService")
-- Register & Set Collision Groups for our Characters
PhysicsService:RegisterCollisionGroup("Players")
PhysicsService:CollisionGroupSetCollidable("Players", "Players", false)
local function OnCharacterAdded(Character: Model): ()
-- Not a great way, but we get back the Player that owns this Character
local Player: Player = Players:GetPlayerFromCharacter(Character)
-- See if the Character is fully loaded (All its Parts & Accessories are added)
if not Player:HasAppearanceLoaded() then
-- If it isn't we Yield until the Event Fires
Player.CharacterAppearanceLoaded:Wait()
end
-- Loop through each Descendant and set each Part's CollisionGroup
for _, Descendant in Character:GetDescendants() do
if Descendant:IsA("BasePart") then
Descendant.CollisionGroup = "Players"
end
end
end
local function OnPlayerAdded(Player: Player): ()
-- If the Character is already added, we just run the callback right away
if Player.Character then
OnCharacterAdded(Player.Character)
end
-- Connect the callback so it runs the next time
Player.CharacterAdded:Connect(OnCharacterAdded)
end
Players.PlayerAdded:Connect(OnPlayerAdded)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment