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
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.
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)