local playerCollision = {}
local PhysicsService = game:GetService("PhysicsService")
local Players = game:GetService("Players")
PhysicsService:CreateCollisionGroup("Players")
PhysicsService:CollisionGroupSetCollidable("Players", "Players", false)
playerCollision.currentCharacterCollisionGroup = {}
local previousCollisionGroups = {}
local collisionGroupSet = {}
function playerCollision.setCharacterCollisionGroup(character, collisionGroupName)
playerCollision.currentCharacterCollisionGroup[character] = collisionGroupName
if character:IsA("BasePart") then
PhysicsService:SetPartCollisionGroup(character, collisionGroupName)
if not collisionGroupSet[character] then
previousCollisionGroups[character] = character.CollisionGroupId
collisionGroupSet[character] = true
end
end
for _,descendant in pairs(character:GetDescendants()) do
if descendant:IsA("BasePart") then
PhysicsService:SetPartCollisionGroup(descendant, collisionGroupName)
if not collisionGroupSet[descendant] then
previousCollisionGroups[descendant] = descendant.CollisionGroupId
collisionGroupSet[descendant] = true
end
end
end
end
function playerCollision.setPartCollisionGroup(object, collisionGroupName)
if object:IsA("BasePart") then
if not collisionGroupSet[object] then
previousCollisionGroups[object] = object.CollisionGroupId
collisionGroupSet[object] = true
end
PhysicsService:SetPartCollisionGroup(object, collisionGroupName)
end
end
function playerCollision.resetCollisionGroup(object)
print("hpaodsf")
if object:IsA("BasePart") then
local previousCollisionGroupId = previousCollisionGroups[object]
if not previousCollisionGroupId then return end
collisionGroupSet[object] = nil
local previousCollisionGroupName = PhysicsService:GetCollisionGroupName(previousCollisionGroupId)
PhysicsService:SetPartCollisionGroup(object, previousCollisionGroupName)
end
end
function playerCollision.run()
local function onCharacterAdded(character)
playerCollision.setCharacterCollisionGroup(character, playerCollision.currentCharacterCollisionGroup[character])
end
local function onPlayerAdded(player)
local characterFirstAddedEvent
characterFirstAddedEvent = player.CharacterAdded:Connect(function(character)
playerCollision.setCharacterCollisionGroup(character, "Players")
character.DescendantAdded:Connect(function(object)
playerCollision.setPartCollisionGroup(object, playerCollision.currentCharacterCollisionGroup[character])
end)
character.DescendantRemoving:Connect(playerCollision.resetCollisionGroup)
characterFirstAddedEvent:Disconnect()
player.CharacterAdded:Connect(onCharacterAdded)
end)
end
Players.PlayerAdded:Connect(onPlayerAdded)
end
return playerCollision
Error at line 27, the problem is that I use the character as an index in a table, but when the character dies and respawns, I can no longer use it as an index because it’s a new model.
What do I do? I don’t want to use the player instead because I think it’s a bad practice, since it won’t work if a player has multiple characters.
What do I use as an index instead?