NPC's collision is off, but when i playtest it turns back on

Hello, so pretty much I made a custom rigged npc that works and has an idle animation. The only problem, is that I don’t want the player to be able to collide with them, so i went through all of the npc’s parts and turned cancollide off. The issue is that when I playtest, the npc’s uppertorso and lowertorso suddenly have cancollide on again and the player can collide with those 2 parts again. I searched through all of my scripts (ctrl+shift+f) and I don’t have a single script that even messes with cancollide or collisions at all.
I am hoping there is a more simple, airtight, and efficient way of doing this because if i am in game and I manually turn cancollide off for both of those parts, it works exactly as its supposed to.

2 Likes

Make a separate collision group for the npc and the player.

Try using CollisionGroups instead. It’s not a very good idea to turn CanCollide off completely, because then your NPC might fall through the ground if the roblox Humanoid object didn’t set HumanoidRootPart to CanCollide (which is why the humanoid object works).

It is a little tricker, but really after the inital setup theres no more hassle and it’ll work like magic. Just setup 2 collision groups like in the linked documentation above, and check for player CharacterAdded connections to set the collision group.

Here’s some code to help you with setting the player character collision groups; for your NPCs you can set them up manually in Studio with the CollisionGroups window. Make your collision groups in Studio and configure them there first. Like this.

And this is the code that will apply the CollisionGroup to the Player characters whenever they are added.

local PLRS = game:GetService('Players')
local PlayersCollisionGroupName = 'Players' -- Change this to whatever you've named your collision group to

local function PlayerAdded(plr)
    local function CharacterAdded(char)
        for _, Part: BasePart in char:GetChildren() do
            if not Part:IsA('BasePart') then continue end
            Part.CollisionGroup = PlayersCollisionGroupName
        end
    end

    CharacterAdded(plr.Character or plr.CharacterAdded:Wait())
    plr.CharacterAdded:Connect(CharacterAdded)
end

for _, plr in PLRS:GetPlayers() do
    PlayerAdded(plr)
end
PLRS.PlayerAdded:Connect(PlayerAdded)

And set your NPC character parts to your other collision group, NoTouchPlayers in my setup. Like this. Keep in mind, you can just select all the rig parts in your NPC and press the "+" button next to your "NoTouchPlayers" collision group (in the collision groups window), and you won’t have to type it in the Properties window manually.

1 Like

Even if that would fix it, it doesn’t explain why cancollide magically turns on every time i playtest with no outside reason. I also figured out even when I manually turn it off, a couple seconds later it turns back on. I know that I probably have to use collision groups but this is very strange.

Roblox turns it on automatically.

Why would roblox automatically turn cancollide for 2 random parts in a custom rig? I deleted the rig of it to see if it would still have collision and sure enough, just that part and lowertorso have collision, and no other parts(the rest fell through the ground). They have no relation to the humanoid root part and yet they still automatically turn collision on. Im just confused more than anything at this point because I now know that I should probably just cut my losses and use collision groups.

I’m not sure about the other one, but Roblox automatically turns it on for LowerTorso.

Good to know, I haven’t done much with rigs or animation on anything really so thank you.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.