How to make follow NPC stop in front of player?

I have this NPC that follows the player but, I want it to stop directly in front of the player a few studs away, instead of going directly to him, here is what I have at the moment.

Here is the A.I itself:

local myHuman = script.Parent:WaitForChild("Humanoid")
local myRoot = script.Parent:WaitForChild("HumanoidRootPart")
local head = script.Parent:WaitForChild("Head")

local clone = script.Parent:Clone()

for _, Part in pairs(script.Parent:GetDescendants())do
	if Part:IsA("BasePart") then
		Part:SetNetworkOwner()
	end
end

function findPath(target)
	local path = game:GetService("PathfindingService"):CreatePath()
	path:ComputeAsync(myRoot.Position,target.Position)
	local waypoints = path:GetWaypoints()
	
	if path.Status == Enum.PathStatus.Success then
		for _, waypoint in ipairs(waypoints) do
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				myHuman.Jump = true
			end
			myHuman:MoveTo(waypoint.Position)
			local timeOut = myHuman.MoveToFinished:Wait(1)
			if not timeOut then
				myHuman.Jump = true
				findPath(target)
				break
			end
			
			if checkSight(target) then
				repeat
					myHuman:MoveTo(target.Position)
					wait(0.1)
					if target == nil then
						break
					elseif target.Parent == nil then
						break
					end
				until checkSight(target) == false or myHuman.Health < 1 or target.Parent.Humanoid.Health < 1
				break
			end
			if (myRoot.Position - waypoints[1].Position).magnitude > 5 then
				findPath(target)
				break
			end
		end
	end
end

function checkSight(target)
	local ray = Ray.new(myRoot.Position, (target.Position - myRoot.Position).Unit * 40)
	local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})
	if hit then
		if hit:IsDescendantOf(target.Parent) and math.abs(hit.Position.Y - myRoot.Position.Y) < 8 then
			return true
		end
	end
	return false
end

function findTarget()
	local dist = 200
	local target = nil
	local potentialTargets = {}
	local seeTargets = {}
	for i,v in ipairs(workspace:GetChildren()) do
		local human = v:FindFirstChild("Humanoid")
		local torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart")
		if human and torso and not human:FindFirstChild("Enemy") then
			if (myRoot.Position - torso.Position).magnitude < dist and human.Health > 0 then
				table.insert(potentialTargets,torso)
			end
		end
	end
	if #potentialTargets > 0 then
		for i,v in ipairs(potentialTargets) do
			if checkSight(v) then
				table.insert(seeTargets, v)
			elseif #seeTargets == 0 and (myRoot.Position - v.Position).magnitude < dist then
				target = v
			end
		end
	end
	if #seeTargets > 0 then
		dist = 200
		for i,v in ipairs(seeTargets) do
			if (myRoot.Position - v.Position).magnitude < dist then
				target = v
			end
		end
	end
	return target
end

function main()
	local target = findTarget()
	if target then
		findPath(target)
	end
end

while wait(0.1) do
	if myHuman.Health < 1 then
		break
	end
	main()
end
2 Likes

hey, i had the same problem with keeping some distance between the NPC and a player, so here’s what i have. you used PathFindingService but i used humanoid:MoveTo() but maybe it could help you out:

while true do
	wait(0.1)
	if (NPC.Torso.Position - PlayerPos.Position).Magnitude > 4 then
		NPC.Humanoid:MoveTo(PlayerPos.Position)
	else
		NPC.Humanoid:MoveTo(NPC.Torso.Position)
	end
end

NPC would be the NPC and the PlayerPos would be the player’s position

1 Like

Try offsetting the position when the NPC is critically near on you or make a invisible part in front for your character few studs away.

Edit: the part must be welded or else it’ll fall then set the CanCollide to false so it wouldn’t collide to anything

1 Like

@reesemiester8 I would recommend using magnitude to check the distance between the player and the NPC’s humanoid. Here is a DevHub article for you to get a better understanding

Hope it helps :slight_smile:

You could make where the NPC magnitude hits 4 or more and when the player stops. you can add a weld to the NPCs’ legs to the baseplate or the part that it standing on. that would stop the NPC from moving. and turn off the stop the walking animation and play idle animation.

1 Like