Hey, So I am trying to make some zombies wonder the world freely all they want, I only want the zombies to start following you when they see you but once you get away and hide they start randomly roaming again, is this possible? If so can someone help me?

I get that for zombies/NPCs to follow you, you use pathfindingservice but I would have no idea how the heck to get the rest of that in.

This script here should make an NPC follow you, how do I make it so it does what I said above??? Please help.

-- In Script in ServerScriptService
 
local pathfindingService = game:GetService("PathfindingService")
local pointModel = Instance.new("Model")
pointModel.Name = "Points"
pointModel.Parent = game.Workspace
 
-- Setup remote function
local remoteFunction = Instance.new("RemoteFunction")
remoteFunction.Parent = game.ReplicatedStorage
 
function visualizePath(path)
	-- clear old path visualization
	pointModel:ClearAllChildren()
 
	-- calculate new visualization	
	local points = path:GetPointCoordinates()
	for _, point in ipairs(points) do
		local part = Instance.new("Part")
		part.FormFactor = Enum.FormFactor.Custom
		part.Size = Vector3.new(1,1,1)
		part.Position = point
		part.Anchored = true
		part.CanCollide = false
		part.Parent = pointModel
	end
end
 
game.ReplicatedStorage.RemoteFunction.OnServerInvoke = function(player, target)
	local start = player.Character.Torso.Position
	local path = pathfindingService:ComputeRawPathAsync(start, target, 500)
 
	visualizePath(path)
	-- Check to see if the path was computed correctly
	if path.Status == Enum.PathStatus.FailStartNotEmpty or path.Status == Enum.PathStatus.FailFinishNotEmpty then
		print("Compute failed")
		return {}
	end
 
	return path:GetPointCoordinates()		
end