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?
Thank you, sorry for making duplicated question
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).
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);
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?
It must be in Script
. Did you get any errors? and What is not working?
I have no errors, I have it in a regular script and also in Workspace, my NPC still bumps into the player though…
Because you didn’t assign your NPC’s to the collision group. Do this:
setNPCGroup() -- example: setNPCGroup(myNPC)
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
Does the setNPCGroup( > ‘myNPC’ < ) need to be the name of the NPC, or can it be anything, and how would that change the script?
Oh alright. Hold on then.
I will do it now.
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.
Wait… So I have to individually set each part of the NPC’s body to the Collision Group?
Alright, so I tried putting
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?
Alright, now I understand, thank you!
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.