How would I make a part CanCollide true for players and false for NPC's?

I am working on a tower defense game, the enemies are all custom characters with Humanoids which spawn and exit through a portel and proceed down a path, the issue is I don’t want players being able to enter the portal so I would like to add a barrier that only the NPC’s can pass through.

Add your NPC’s to a collision group where they don’t collide with your portal, so you can leave it CanCollide true normally so player’s don’t go through it but your npc’s do

1 Like

you could add a local script that creates a part in front of the portal

local barrier = Instance.new("Part")
barrier.Transparency = 1
barrier.Anchored = true
barrier.Size = Vector3.new() -- Put how big you need the part for it to be able to cover the area
barrier.Position = Vector3.new() -- Put the position of where you want the berrier to be

You can use physics collision groups for this.

local physics = game:GetService('PhysicsService')

physics:CreateCollisionGroup("NPC") -- Creating a group named "NPC"
physics:CollisionGroupSetCollidable("Default","NPC",false) -- Settings its collision

-- Adding a part to the group
physics:SetPartCollisionGroup(Part, "NPC")

-- Adding a model to the group
for _, v in ipairs(Model:GetDescendants()) do
	if v:IsA("BasePart") then
		physics:SetPartCollisionGroup(v, "NPC")
	end
end

As you can see at the top, there are two lines, one that creates a group and the next sets its collision.
You can copy this if you want more collision groups.

I gave two example on how to add parts/models to the collision groups. The collision group stays the same even after you clone it. Meaning, you will just set the collision group of the original model, then clone it.

English is not my main language so I’m sorry if my grammar is bad or you didn’t understand something

2 Likes

You could get the player.

local Player = game.Players:GetPlayerFromCharacter(Character)

if Player then

else --if character doesn't have a player

end

Actually this wouldn’t fit your case, because players could enter the area while an NPC is noclipping.

Go for @Lielmaster’s method instead.

You could turn off collide on the NPC once they reach a certain spot.