How would I go about making NPCs unable to be pushed by players. I have tried anchoring the the humanoidrootpart but that disable the NPCs idle animation and I have tried changing the collisiongroupid but thats pointless because it just falls through the map, anyone have any suggestions?
This is off the top of my head, night be incorrect, maybe a invisible box?
I thought about that but I was wondering if there was a less clunky way to do it, if I can’t find another solution I will use that though, thanks.
You should use PhysicsService to achieve this. You can assign the npc and players into their own collision groups and have the two groups not collide with each other.
I can write a quick script for you if you wish once I am on my computer.
That would be greatly appreciated if you don’t mind, thanks.
Create a model or folder under workspace called ‘Bodies’.
Any character you add to that folder will not collide with other characters inside that folder.
This also makes it so that players do not collide with other players or NPCs inside that folder.
Hope this helps!
local PhysicsService = game:GetService("PhysicsService")
local Players = game:GetService("Players")
local playerCollisionGroupName = "Players"
PhysicsService:CreateCollisionGroup(playerCollisionGroupName)
PhysicsService:CollisionGroupSetCollidable(playerCollisionGroupName, playerCollisionGroupName, false)
local previousCollisionGroups = {}
local function setCollisionGroup(object)
if object:IsA("BasePart") then
previousCollisionGroups[object] = object.CollisionGroupId
PhysicsService:SetPartCollisionGroup(object, playerCollisionGroupName)
end
end
local function setCollisionGroupRecursive(object)
setCollisionGroup(object)
for _, child in ipairs(object:GetChildren()) do
setCollisionGroupRecursive(child)
end
end
local function resetCollisionGroup(object)
local previousCollisionGroupId = previousCollisionGroups[object]
if not previousCollisionGroupId then return end
local previousCollisionGroupName = PhysicsService:GetCollisionGroupName(previousCollisionGroupId)
if not previousCollisionGroupName then return end
PhysicsService:SetPartCollisionGroup(object, previousCollisionGroupName)
previousCollisionGroups[object] = nil
end
local function onCharacterAdded(character)
setCollisionGroupRecursive(character)
character.DescendantAdded:Connect(setCollisionGroup)
character.DescendantRemoving:Connect(resetCollisionGroup)
end
local function onPlayerAdded(player)
player.CharacterAdded:Connect(onCharacterAdded)
end
for i,v in pairs(workspace.Bodies:GetChildren()) do
setCollisionGroupRecursive(v)
end
workspace.Bodies.ChildAdded:Connect(function(added)
setCollisionGroupRecursive(added)
end)
Players.PlayerAdded:Connect(onPlayerAdded)
Note:
This is not 100% my code.
I edited it to make it so that NPCs inside the Bodies folder don’t collide.
it is pretty basic stuff.
Did you anchor the NPC? This could be the reason.
You can simply make use of BodyPosition and BodyGyro to keep the NPC in place (assuming you know how those work).
You can simply work with the collisiongroupId’s. Just make the players not collidable with the npcs. The NPC won’t fall trough the map if you configure the collisiongroups correctly :))
I’ve done this in this test place for swordfighting AIs, there’s an entertainment corner where you will see 4 holograms swordfighting eachother. This AIs don’t interact at all with players, and this can be achieved in two ways,
Scripting
Specifically PhysicsService and CollisionGroups.
In my case, I have 2 folders, one folder for player characters, and the other folder for the holograms.
Player’s Folder : PLAYER_FOLDER
Holograms : HOLOGRAM_FOLDER
Both of these are parented to workspace, and when I spawn this NPCs I put them in that folder, as for the players, I move their character into the player folder after they spawn. And that brings us to the last objective, and that is the coding process, which is as simple as this,
local PS = game:GetService'PhysicsService'
local CG_Players = PS:CreateCollisionGroup'PLAYERS'
local CG_Holograms = PS:CreateCollisionGroup'HOLOGRAMS'
PS:CollisionGroupSetCollidable('PLAYERS','Default',true)
PS:CollisionGroupSetCollidable('HOLOGRAMS','Default',true)
PS:CollisionGroupSetCollidable('PLAYERS','HOLOGRAMS',false)
local function set_CG(char,id)
for i,v in pairs(char:GetDescendants())do
if v:IsA'BasePart' or v:IsA'UnionOperation' or v:IsA'MeshPart' then
v.CollisionGroupId = id
end
end
end
workspace.PLAYER_FOLDER.ChildAdded:Connect(function(c)
set_CG(c,CG_Players)
end)
workspace.HOLOGRAM_FOLDER.ChildAdded:Connect(function(c)
set_CG(c,CG_Holograms)
end)
Collision Filtering
This handy icon in studio here called Collision Groups under the MODEL tab. It will open up a very easy-to-use Collision Group Editor
My god, I’d been manually assigning collision groups with code…
I have two questions. The first one is, how do I add characters to the folder? Also, where do I put the script?
I know this is old but my method making the NPC not move is anchoring the torso. the animations still plays but the torso wouldn’t animate only the arms head and legs it will keep the Collision too so you can still step on his head. I hope this helps.
Turn off collisions, then the players can’t touch them at all.
Didnt work. Starting to give up on my project.
did not know it was 8 months ago sorry lol
Be more specific when posting for help. What specifically didn’t work? What have you tried? Providing details doesn’t guarantee that someone will help, but not providing details basically guarantees no one can help.