[HELP pls] Collision groups not functioning as expected

I’m working on an AI system and I have barriers around the map

I want it so the barriers are ONLY non-collidable for the NPC characters, not for players. I have been looking on the browser for some time and I cannot find somebody that is not doing (player - npc) collisions, anyway I don’t got much to say I’m really frustrated and confused on why it’s not working, here’s my code:

local physicsservice = game:GetService("PhysicsService")

local sidewalktings = game.Workspace.AIStuff.Barriers.Sidewalk

local NPCCollisions = "NPCs"
local SidewalkBarriers = "SidewalkBarriers"

physicsservice:CreateCollisionGroup(NPCCollisions)
physicsservice:CreateCollisionGroup(SidewalkBarriers)

physicsservice:CollisionGroupSetCollidable(NPCCollisions, SidewalkBarriers, false)
physicsservice:CollisionGroupSetCollidable(SidewalkBarriers, NPCCollisions, false)

for i, v in pairs(game.Workspace.AIStuff.AIs:GetChildren()) do
	
	spawn(function()
		
		setnpcingroup(v)
		
	end)
	
end

for i, v in pairs(sidewalktings:GetChildren()) do
	
	physicsservice:SetPartCollisionGroup(v, SidewalkBarriers)
	
end

game.Workspace.AIStuff.AIs.ChildAdded:Connect(function(child)
	
	setnpcingroup(child)
	
end)

function setnpcingroup(child)
	
	for i, v in pairs(child:GetChildren()) do

		if v:IsA("Part") or v:IsA("MeshPart") then

			physicsservice:SetPartCollisionGroup(v, NPCCollisions)
			
			print("Done")

		end

	end
	
end

I clearly have no idea what’s not working

Just to clarify, Collision groups set to true doesn’t work as well

try to use collision groups using the studio tab instead of scripts. This is much easier to do as you just create a new collision group then highlight the objects you need to apply in collision group then check the boxes of the objects you want them to collide with.
note: I didn’t try scripting it though. This is just how I made it for electric door mechanisms.

1 Like

Where to find the tab? can’t seem to see it in view

Go to model then far right then top row in the middle. It should look like two balls about to hit each other

I found the link to my reference: Collision Filtering

1 Like

Yea my bad I’m not really a big fan of the studio configuration, thanks tho I’ll wait for someone to help me out with the code

(bump so I pop back up for a chance of help)

(last bump before I leave this thread, I need help)

It seems like you didn’t use the PhysicsService:CollisionGroupsSetCollidable(group1, group2, true or false) function.

If you want NPCs to go through barriers then you must do the following:


PhysicsService:CollisionGroupsSetCollidable(NPCCollisions, SidewalkBarriers, False)

PhysicsService:CollisionGroupsSetCollidable(PlayerGroup, SidewalkBarriers, True) -- Players can't go pass the barriers no more.

You also didn’t create a collision group for all the players in the game, you will need to loop through the children of the character to get every basepart and insert it into the collision group, like so:


PhysicsService:CreateCollisionGroup("PlayerGroup")

local function AddPlayerToCollisionGroup(char)
       for i, v in pairs(char:GetChildren()) do

            if v:IsA("Basepart") then
                PhysicsService:SetPartCollisionGroup(v, "PlayerGroup")
              AddPlayerToCollisionGroup(v)
        end
  end
end

game.Players.PlayerAdded:Connect(function(plr)
        
      local char = plr.Character or plr.CharacterAdded:Wait() -- get the character model of the player

AddPlayerToCollisionGroup(char)
end)

(By the way, I typed this on mobile so the lines in the code snippet might be positioned incorrectly or something)

You are very wrong for this, read what I’m trying to do

Could you state where I’ve gone wrong?

What I’m trying to achieve is for NPC characters not to be able to go through the parts in the group

You need to set this to true not false.

I already stated on the first reply that on true it doesn’t work aswell

Does the function setnpcinggroup work? It seems like you executed the function before making it.

image

I debugged it, it groups all 16 parts of character

I don’t know if this is an engine bug, but pathfindingservice totally ignores parts that are not meant to pass through

Now I don’t know how to achieve AI walking on sidewalks / crosswalks

Did you try setting these 2 functions to true?

Yep I did, it made the same behaviour, it just passed through the barriers