0

# How Do You Make NPCs Not Collide With Each Other?

Edited 4 years ago

I'm making something where the npcs will walk on a path. The problem is, the npcs keep running into each other and clogging up the paths.

In the studio, I tried turning off CanCollide in all of the body parts, but when I tested the game, I was surprised to find that the npcs were colliding with each other still! I then looked in the npcs to see that CanCollide was turned back on. I made a script that would turn it off if it was set to true, but the npcs still were colliding with each other and CanCollide was just turning off and back on constantly.

I've seen npcs not collide with each other in other games so I know it is possible, but I'm just not sure how.

I thought it might have to do with the humanoid but I looked through all the properties but didn't see any values that I would suspect to be keeping CanCollide on.

0
Thanks! I actually found that like 5 minutes before you posted that comment just so I could answer my own question. I tried to make it detailed so if anyone looks at this with the same question they will get a detailed answer! Thank you again since if I didn't find it before I would of found out from that comment Adv3rtizement 101 — 4y

1

I actually found out how to fix this myself! I just used collision filtering.

It allows you to make it so certain groups don't collide with each other! There is a detailed tutorial on disabling player to player collisions but I just took some things from it to make it for NPCs (Non-Player-Characters).

Here is an example script:

local PhysicsService = game:GetService("PhysicsService") --Gets the physics service.
npcGroup = "npcGroup"
PhysicsService:CreateCollisionGroup(npcGroup) --Creating the actual collison group for all npcs!
PhysicsService:CollisionGroupSetCollidable(npcGroup, npcGroup, false) -- Making the group not be able to collide with itself

local function setCollisionGroupRecursive(object) --
if object:IsA("BasePart") then -- If the object is a part then...
PhysicsService:SetPartCollisionGroup(object, npcGroup) -- ...add it to the group!
end
for _, child in ipairs(object:GetChildren()) do -- For all children in the object (if it has any)...
setCollisionGroupRecursive(child) -- ...Make them go through the function to check if they are a part to be added to the group.
end
end

npc = workspace.NPC
setCollisionGroupRecursive(npc) -- Gives the function the value of the npc as the object!
-- Everything in the npc that is a "BasePart" should be entered into the collision group and shouldn't be able to collide with any other npcs in that group!