How to make NPC Collide with certain part

So, I found a script that make npc uncollide with anything, but is there a way to make npc collide with certain part?

–script

local physicsService = game:GetService('PhysicsService')

physicsService:CreateCollisionGroup('NPCs') -- the NPC collision group
-- we don't have to define a collision group for the workspace since the default one already exists, it's called "Default"

physicsService:CollisionGroupSetCollidable('NPCs', 'Default', false) -- set the NPCs collision group to not collide with the default collision group
physicsService:CollisionGroupSetCollidable('NPCs', 'NPCs', false) -- set the NPCs collision group to not collide with other NPCs

local function setNPCCollisionGroup(npc: Model) -- this function will take an NPC and add all of its BasePart descendants to the NPCs collision group
    for i,v in ipairs(npc:GetDescendants()) do
        if not v:IsA('BasePart') then
            continue
        end
        physicsService:SetPartCollisionGroup(v, 'NPCs')
    end
end

local newNPC = workspace:WaitForChild("CapyBara")
setNPCCollisionGroup(newNPC) -- now with our new NPC, call the function to set all of its descendants to the NPCs collision group
1 Like

Have you followed the tutorial that are provided from Roblox?

So basically, Collision group will define what can you collide with based on the name of your group. For example, if you set a new group name called ‘HumanZone’ and assign an NPC to be collide-able with this group, your NPC will be able to walk through the zone. These settings can be manually configured through Collision Editor

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.