How to make npcs collide with models but not with players?

Hey,

how can I make that npcs can collide with models but not with players? I want that the player can walk through him but the npc is still standing on a part.

https://developer.roblox.com/en-us/articles/Collision-Filtering

Example code:

local PhysicsService = game:GetService("PhysicsService")
local Players = game:GetService("Players")
local NPCS = {workspace.Dummy}
PhysicsService:CreateCollisionGroup("Players")
PhysicsService:CreateCollisionGroup("NPCS")

PhysicsService:CollisionGroupSetCollidable("Players", "NPCS", false)

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		for i, instance in pairs(character:GetDescendants()) do
			if instance:IsA("Part") or instance:IsA("BasePart") then
				PhysicsService:SetPartCollisionGroup(instance, "Players")
			end
		end
	end)
end)

for i, NPC in pairs(NPCS) do
	for i, instance in pairs(NPC:GetDescendants()) do
		if instance:IsA("Part") or instance:IsA("BasePart") then
			PhysicsService:SetPartCollisionGroup(instance, "NPCS")
		end
	end
end
1 Like

This doesnt work, I dont know what to do

I literally have been trying to find an actual working/non-useless piece of code that is not broken for 2 hours.

Sorry for bumping, but that code works, if you add a (task.) wait(0.5) after the character is added.
So like this:

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
      task.wait(0.5) -- or wait()
		for i, instance in pairs(character:GetDescendants()) do
			if instance:IsA("BasePart") then -- why even using IsA(Part)? A Base Part holds all Types of Parts (Mesh,Part,etc.) 
				PhysicsService:SetPartCollisionGroup(instance, "Players")
			end
		end
	end)
end)
1 Like