How to disable collision between NPCs and Players?

I am trying to disable collisions between NPCs and Players (like in Tower Battles) . I tried by myself and I only achieve that players don’t collide with baseplate. Does anybody can help me figure it out?

10 Likes
2 Likes

Thank you, sorry for making duplicated question

2 Likes

This script does not prevent collisions between player characters and non-player characters. It only prevents collisions between player characters (so will not solve this thread).

4 Likes

You may want to try creating different collision groups. Here is a modified example from the wiki that you could try.

local PhysicsService = game:GetService("PhysicsService")
 
local defaultGroup = PhysicsService:GetCollisionGroupName(0);
local playerGroup = "PlayerGroup";
local npcGroup = "NPCGroup";
 
PhysicsService:CreateCollisionGroup(playerGroup);
PhysicsService:CreateCollisionGroup(npcGroup);

function setNPCGroup(char)
    for i,v in pairs(char:GetDescendants()) do
        if (v:IsA("BasePart")) then
            PhysicsService:SetPartCollisionGroup(v, npcGroup);
        end
    end
end

function setPlayerGroup(char)
    for i,v in pairs(char:GetDescendants()) do
        if (v:IsA("BasePart")) then
            PhysicsService:SetPartCollisionGroup(v, playerGroup);
        end
    end
end

-- Handle NPC creation.

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        setPlayerGroup(char);
    end)
end)

PhysicsService:CollisionGroupSetCollidable(playerGroup, npcGroup, false);
PhysicsService:CollisionGroupSetCollidable(playerGroup, playerGroup, true);
PhysicsService:CollisionGroupSetCollidable(npcGroup, npcGroup, true);
PhysicsService:CollisionGroupSetCollidable(playerGroup, defaultGroup, true);
PhysicsService:CollisionGroupSetCollidable(npcGroup, defaultGroup, true);
11 Likes

It doesn’t work for me and I honestly don’t know why… Can you be more specific if I need to create any folders, or even if I need to put it in a LocalScript or not?

1 Like

It must be in Script. Did you get any errors? and What is not working?

1 Like

I have no errors, I have it in a regular script and also in Workspace, my NPC still bumps into the player though…

1 Like

Because you didn’t assign your NPC’s to the collision group. Do this:

setNPCGroup() -- example: setNPCGroup(myNPC)
2 Likes

How would I go about doing that? Is it a variable? I tried adding my NPC to collision group through the visual editor, but I have no idea how it works.

Add it by using the setNPCGroup() function. You can make a folder for your NPC’s and add them using this code.

local NPCfolder = workspace.myNPCfolder
for _, v in pairs(NPCfolder:GetChildren()) do setNPCGroup(v) end
1 Like

Does the setNPCGroup( > ‘myNPC’ < ) need to be the name of the NPC, or can it be anything, and how would that change the script?

1 Like

Oh alright. Hold on then.

I will do it now.

1 Like

No, it must be an object. If you look closely on this part of a script:

function setNPCGroup(char)
    for i,v in pairs(char:GetDescendants()) do -- go through each instance of this object (most likely an npc)
        if (v:IsA("BasePart")) then -- check if that instance is a part
            PhysicsService:SetPartCollisionGroup(v, npcGroup); -- assign that part to the collision group
        end
    end
end

You can see that, it takes an object.

1 Like

Wait… So I have to individually set each part of the NPC’s body to the Collision Group?

1 Like

Alright, so I tried putting image
inside of my NPC (If I understood you correctly)
and


is located in the Workspace…

and also created 2 Folders called ‘NPCGroup’ and ‘PlayerGroup’.

Is this correct?

1 Like

Here is the solution: CollisionExample.rbxl (24.7 KB)

3 Likes

Alright, now I understand, thank you!

1 Like

I stumbled on this thread but still couldn’t get even the provided .rbxl to work. My character is wearing packages which load in after the CharacterAdded event fires and reset the CollisionGroupId back to 0. So the solution for me was to change the CharacterAdded event to the CharacterAppearanceLoaded event.

Make sure you use this or players with certain outfits may still collide.

For some reason, it didn’t work for me? I set the NPC group correctly and even printed the body parts in the setNPCGroup function to test. The collision still wasn’t working.