I cannot figure out how to make CollisionGroups work

Hello developers,

I want to make an NPC to be able to walk through walls but im having an issue with making it happen. If anybody could help me, It would be really appreciated! By the way, the script is inside the NPC.

local ps =game:GetService("PhysicsService")

ps:CreateCollisionGroup("WallsGroup")
ps:CreateCollisionGroup("NPC")

for _,v in pairs(game.Workspace:GetChildren()) do
	if v:IsA("BasePart") then
		ps:SetPartCollisionGroup(v,"WallsGroup")
	end
end

for _,npc in pairs(script.Parent:GetChildren()) do
	for _,v in pairs(npc:GetDescendants()) do
		if v:IsA("BasePart") then
			ps:SetPartCollisionGroup(v,"NPC")
		end
	end
end

wait(2)

ps:CollisionGroupSetCollidable("WallsGroup", "NPC", false)

Is your script inside a single npc or a group of npcs? From the way your second loop is structured it looks like your script assumes its in a group of them. You probably also don’t want to set every part in the workspace to be a wall, otherwise it’ll result in your npcs falling through the floor.

1 Like

Its inside 1 Npc and i made it to clip throught all the parts in the workspace just so i could test it.

Once i switch it to :GetChildren, it does an error, “attempt to index number with ‘IsA’”

image

Ideally, the script should be placed in the Server Script Service. If you simply move the script and the pointers (script.Parent) you’ll achieve the same result.

From briefly running over your code, I don’t see any issue with how the collision groups are being set etc. However what you need to make sure is that the pathfinding is actually recognising the doors as not existing - I am pretty certain that pathfinding will see a wall and assume that you cannot pass through the wall as it doesn’t actually check if the wall is no collide.

Therefore please make sure your pathfinding has been given relevant modifiers so it can understand which objects can be passed through.

Now I’m not just going to abandon you…

Here is Roblox API documentation which actually explains passing through a door with Roblox’s pathfinding:

:question: View ignoring obstacles.

it should be npc:IsA(“BasePart”)

1 Like

v is the index in that circumstance, therefore you cannot do :IsA() on it :slight_smile:

2 Likes

Thank you both, it is now working! :slightly_smiling_face: