So I am trying to make a player and an npc non-collide. Here is my script there are no errors:
game.Players.PlayerAdded:Connect(function(plr)
local npcGroup = "NPCS"
local playerGroup = "WeThePeople"
PhysicsService:RegisterCollisionGroup(npcGroup)
PhysicsService:RegisterCollisionGroup(playerGroup)
PhysicsService:CollisionGroupSetCollidable(npcGroup, playerGroup, false)
for i, v in pairs(NPCS:GetDescendants()) do
if v:IsA'BasePart' then
v.CollisionGroup = npcGroup
end
end
for i, v in game.Players:GetDescendants() do
if v:IsA'BasePart' then
v.CollisionGroup = playerGroup
end
end
end
Instead of getting the Descendants of players, do GetDescendants on the character when its added. You should also register the Collision Groups outside, so we dont do it everytime a new player gets added. Same for the Npcs, they dont need to be added each time a player gets added all over again. If Npcs gets spawned in and such, you can check if a child has been added to the npc folder and then add them to the collisiongroup.
local npcGroup = "NPCS"
local playerGroup = "WeThePeople"
PhysicsService:RegisterCollisionGroup(npcGroup)
PhysicsService:RegisterCollisionGroup(playerGroup)
PhysicsService:CollisionGroupSetCollidable(npcGroup, playerGroup, false)
for i, v in pairs(NPCS:GetDescendants()) do
if v:IsA'BasePart' then
v.CollisionGroup = npcGroup
end
end
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(character)
for i, v in character:GetDescendants() do
if v:IsA'BasePart' then
v.CollisionGroup = playerGroup
end
end
end)
end)