Make NPC Stop Following a Set Path and Follow Player

Hello! So I’m setting up a system for NPCs. I want them to be able to follow a set path, but then chase a player when they are within range. When the player is out of range, I want them to head back to their original path. I’ve got this set up… sort of. But it’s a pretty clunky system, so I’d like to get some suggestions from the community. How would you go about switching between following the path and chasing the player and vice versa?

1 Like

To Make A Pretty Basic System Your Asking.

  • Setup The Variables/Definitons
local Players = game:GetService("Players") --Player Service
local Client = Players.LocalPlayer --The Player


--Get The Players Character Model
local Character = Client.Character Or Client.CharacterAdded:Wait()
local NPC = {} --NPC Table


  • If The NPC Is In Range Of The Player
local function CheckRange(Distance : number)
     for _, Models in ipairs(NPC) do
          if (Models.HumanoidRootPart - Character.HuamnoidRootPart) <= Distance then
                Models:FindFirstChild('Humanoid'):MoveTo(Vector3.new(Character.HumanoidRootPart.Position))
          end     
     end
end
  • If The NPC Is Out Of Range
local function CheckRange(Distance : number)
     for _, Models in ipairs(NPC) do
          if (Models.HumanoidRootPart - Character.HuamnoidRootPart) <= Distance then
                Models:FindFirstChild('Humanoid'):MoveTo(Vector3.new(Character.HumanoidRootPart.Position)) 
          else
               --Continue whatever the NPC(s) were doing
          end     
     end
end

Correct Me If I got something wrong.

1 Like

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