I have this Pathfinding NPC and he moves to the checkpoints I have set, The problem is he can be moved my a player walking into him so he goes of course I want them not to be able to push the NPC around. How would I do this? This is the pathfinding script.
You will need to use the PhysicsService to control CollisionGroups. Add the players and npcs to the relevant groups, then determine who can collide with what.
As an exmaple:
local physicsService = game:GetService("PhysicsService")
-- COLLISION GROUPS
physicsService:CreateCollisionGroup("players")
physicsService:CreateCollisionGroup("npcs")
physicsService:CollisionGroupSetCollidable("players", "players", false)
physicsService:CollisionGroupSetCollidable("npcs", "players", false)
physicsService:CollisionGroupSetCollidable("npcs", "npcs", false)
When a player character or npc spawns, you will need to add them to the relevant group. For example:
local function setNPCCollisions(npc)
collectionService:AddTag(npc, "NPC")
for _, part in pairs(npc:GetChildren()) do
if part:IsA("BasePart") or part:IsA("MeshPart") then
physicsService:SetPartCollisionGroup(part, "npcs") -- Inefficient
end
end
end
-- SET PLAYER COLLISIONS
local function setPlayerCollisions(character)
for _, part in pairs(character:GetChildren()) do
if part:IsA("BasePart") or part:IsA("MeshPart") then
PhysicsService:SetPartCollisionGroup(part, "players") -- Inefficient
end
end
end
Players.PlayerAdded:Connect(function()
player.CharacterAppearanceLoaded:Connect(function(character)
setPlayerCollisions(character)
end)
end)
-- Insert your own bit of code in the NPC spawn and call the setNPCCollisions() func.
Thank you for helping I will try this now.
Where would I put this script Im fairly new to programming and still learning.
Create a server script inside ServerScriptService and put the code in.
You forgot to put the player param on the playerAdded event.
local physicsService = game:GetService("PhysicsService")
local collectionService = game:GetService("CollectionService")
local function setNPCCollisions(npc)
collectionService:AddTag(npc, "NPC")
for _, part in pairs(npc:GetChildren()) do
if part:IsA("BasePart") or part:IsA("MeshPart") then
physicsService:SetPartCollisionGroup(part, "npcs")
end
end
end
-- SET PLAYER COLLISIONS
local function setPlayerCollisions(character)
for _, part in pairs(character:GetChildren()) do
if part:IsA("BasePart") or part:IsA("MeshPart") then
physicsService:SetPartCollisionGroup(part, "players")
end
end
end
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAppearanceLoaded:Connect(function(character)
setPlayerCollisions(character)
end)
end)
Should work now, make sure to watch the scripting tutorials of alvinblox and thedevking because they teach you good stuff about scripting. Sorry for the late response, hope this helps you
It still does not work is it the name of my Npc? its name is jerry on studio.
setNPCCollisions(workspace.Jerry)
-- please learn basic scripting bruh
I know im bad at programming but where do I put the setNPCCollisions?
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAppearanceLoaded:Connect(function(character)
setPlayerCollisions(character)
end)
end)
setNPCCollisions(workspace.Jerry)
Can you put that with the full script
Just put it under the player added connection…