Solution
The easiest and most efficient way to achieve this would be using collision groups.
Physics service
Most things involving collision groups use the Physics service, which can create collision groups and disable/enable collision between collision groups.
For the purpose of disabling NPC to NPC collision, the :CreateCollisionGroup() function and the :CollisionGroupSetCollidable() function can be used. The former creates a collision group with a given name and the latter enables/disables collisions between two groups or members of the same group
Application
With all said, time to put the functions into effect :3
step 1: reference the physics service
step 2: create an NPC collision group
step 3: disable collisions for members of that group
step 4: add all bodyparts of the NPCs to the NPC collision group
local PS = game:GetService("PhysicsService")
local NPCs = workspace.NPCs
local CollisionGroupName = "NPCs"
PS:CreateCollisionGroup(CollisionGroupName)
PS:CollisionGroupSetCollidable(CollisionGroupName, CollisionGroupName, false)
local function SetCGRecursive (object)
if object:IsA("BasePart") then
PS:SetPartCollisionGroup(object, CollisionGroupName)
end
for _,child in next, object:GetChildren() do
SetCGRecursive(child)
end
end
for _,NPC in pairs(NPCs:GetChildren()) do
SetCGRecursive(NPC)
end
Additional things
Also, as you are making an RPG which probably involves combat, you can use the ChildAdded function to see when something has been parented to another object
Ex:
NPCs.ChildAdded:Connect(function(child)
SetCGRecursive(child)
end)
A couple of reference links:
tutorial for disabling player-player collisions
Physics Service
Generic for loops
Generic for loops 2
recursive functions
child added
Hopefully this helped and have a great day!
2 comments
When do you create the NPC's or are they already in the world? Ideally you would want to add them to another CollisionGroup whenever you create them just like the link you gave.
learning how to adapt related code made by others for your own use is an important skill