Getting a NPC to follow the player after it sees it

  1. What do you want to achieve? Keep it simple and clear!
    I want to make the script shorter if there’s a way and also want to make it so if the NPC sees the player. The NPC Follows the player.

  2. What is the issue? Include screenshots / videos if possible!
    I can’t really find any ways how to get the NPC to stop following parts and to follow player after it sees it.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    YouTube and devforum.

This script is in startercharterscripts and is a local script.


local NPC = workspace.Boss

local Character = game.Players.LocalPlayer.Character

local MoveParts = NPC.MoveParts

local humanoid = NPC:WaitForChild("Humanoid")

local walkAnim = script:WaitForChild("Walk")

local walkAnimTrack = humanoid.Animator:LoadAnimation(walkAnim)

local idleAnim = script:WaitForChild("Idle")

local idleAnimTrack = humanoid.Animator:LoadAnimation(idleAnim)

while true do

NPC.Humanoid:MoveTo(MoveParts.Part.Position)

walkAnimTrack:Play()

idleAnimTrack:Stop()

NPC.Humanoid.MoveToFinished:Wait()

walkAnimTrack:Stop()

idleAnimTrack:Play()

wait(3)

NPC.Humanoid:MoveTo(MoveParts.Part2.Position)

walkAnimTrack:Play()

idleAnimTrack:Stop()

NPC.Humanoid.MoveToFinished:Wait()

walkAnimTrack:Stop()

idleAnimTrack:Play()

wait(3)

NPC.Humanoid:MoveTo(MoveParts.Part3.Position)

walkAnimTrack:Play()

idleAnimTrack:Stop()

NPC.Humanoid.MoveToFinished:Wait()

walkAnimTrack:Stop()

idleAnimTrack:Play()

wait(3)

NPC.Humanoid:MoveTo(MoveParts.Part4.Position)

walkAnimTrack:Play()

idleAnimTrack:Stop()

NPC.Humanoid.MoveToFinished:Wait()

walkAnimTrack:Stop()

idleAnimTrack:Play()

wait(3)

end

RunService.RenderStepped:Connect(function()

local npcToCharacter = (Character.Head.Position - NPC.Head.Position).Unit

local npcLook = NPC.Head.CFrame.LookVector

local dotProduct = npcToCharacter:Dot(npcLook)

if dotProduct > .5 then

--character is in fox

else

--character is not in field of view

end

end)```
3 Likes

You should probably be doing this on the server side for security and so it replicated across to other clients (and I’m not sure the code would even work in a LocalScript)

First, the while true do loop is above other code – that other code won’t run so put any infinite loops at the bottom, second you might want to set some variable that determines if it should follow a player and otherwise follow the path (if I understand your code correctly) but note: you have a lot of :Wait()s, which interferes with the detection, so maybe have a :MoveToFinished callback that checks some status to either continue the path or if theres a target player, move to target player

It seems you already have to code to detect if its in FOV, you just have to “cancel” the MoveTo (maybe there’s a better way but you could just :MoveTo the NPC’s humanoid root part’s position)

Here’s just an idea (its untested and just for reference)

local pathes = {
 -- add points in pathes
}
local pathtarget = 0

NPC.Humanoid.MoveToFinished:Connect(function()
  StopAnim()
  wait(3)
  if pathtarget ~= -1 then -- check if following a player
    pathtarget = ((pathtarget + 1) % #pathes) -- cycle path targets from 0 to length of pathes - 1
    NPC.Humanoid:MoveTo(pathes[pathtarget+1]) -- go to next path target 
  end
end)

function FollowPlayer(playerhrp) -- player humanoid root part
  pathtarget = -1 -- stop following path
  NPC.Humanoid:MoveTo(playerhrp.Position, playerhrp) -- follow player (note this will follow player even if they move)
end
function UnfollowPlayer()
  pathtarget = 0 -- reset path
  NPC.Humanoid:MoveTo(NPC.Humanoid.RootPart.Position) -- cancel follow (also calls MoveToFinished to restart pathes)
end

FollowPlayer and UnfollowPlayer would be called in your RenderStepped

Lastly, would you mind formatting the code like this (surround code with 3 backticks), it makes it easier to read

print("Hello, world")

(while writing it’ll look like this)
```
print(“Hello, world”)
```

1 Like

Thank You! Though am having troubles. The spider doesnt move or follow the path! It is now a script and is in the boss model. I dont really know the problem though the StopAnim() is underline in yellow.

local MoveParts = NPC.MoveParts
local humanoid = NPC:WaitForChild("Humanoid")

local pathes = {
	-- add points in pathes
	MoveParts.Part,
	MoveParts.Part2,
	MoveParts.Part3,
	MoveParts.Part4
}
local pathtarget = 0

NPC.Humanoid.MoveToFinished:Connect(function()
	StopAnim()
	wait(3)
	if pathtarget ~= -1 then -- check if following a player
		pathtarget = ((pathtarget + 1) % #pathes) -- cycle path targets from 0 to length of pathes - 1
		NPC.Humanoid:MoveTo(pathes[pathtarget+1]) -- go to next path target 
	end
end)

function FollowPlayer(playerhrp) -- player humanoid root part
	pathtarget = -1 -- stop following path
	NPC.Humanoid:MoveTo(playerhrp.Position, playerhrp) -- follow player (note this will follow player even if they move)
end
function UnfollowPlayer()
	pathtarget = 0 -- reset path
	NPC.Humanoid:MoveTo(NPC.Humanoid.RootPart.Position) -- cancel follow (also calls MoveToFinished to restart pathes)
end```

My script was more of a reference, you have to put your code to start and stop anims where I put the StopAnim() and StartAnim() functions, in addition, my example code doesn’t actually look through players and check who to follow, that would be separate (but once you do find who to follow, the FollowPlayer and UnfollowPlayer functions could be helpful). My intent with my code was to give some examples to supplement your code – not fully replace it, I can help more but you should take some of your code and add some of mine and see what you get.

One other note: putting parts in the pathes list may not work, I had originally intended it to hold Vector3s (MoveParts.Part.Position)