Need help with Pathfinding

I scripted it so that the Bot stops 6 studs away from the Pathfinding bot but it walks/looks in the other direction .

The problem should be at the end of the script.

— Server Script

local Humanoid = script.Parent.Humanoid
local SpawnPoint = script.Parent.SpawnPosition
local Animator = Humanoid:WaitForChild("Animator")
local NpcHrp = script.Parent.HumanoidRootPart

local PFS = game:GetService("PathfindingService")
local path = PFS:CreatePath()

local Distance
local Nearestplayer
local PlayerDistance 

local Waypoints

while true do -- NPC Cycle
	local ShortestDistance = math.huge 
	
	repeat -- checks every Player and their distance until one is <= 30 away
		for i, player in game.Players:GetPlayers() do
			if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then -- check if Player is ingame
				Distance = (SpawnPoint.Position - player.Character.HumanoidRootPart.Position).Magnitude
				if Distance < ShortestDistance then
					ShortestDistance = Distance
					NearestPlayer = player
				end 
			end
		end
		wait()
	until ShortestDistance <= 30	
	
	repeat
		spawn(function() -- follow player
			repeat	
				for i, player in game.Players:GetPlayers() do
					if NearestPlayer.Character and NearestPlayer.Character:FindFirstChild("HumanoidRootPart") then -- check if Player is ingame
						Distance = (SpawnPoint.Position - NearestPlayer.Character.HumanoidRootPart.Position).Magnitude
						PlayerDistance = (NpcHrp.Position - NearestPlayer.Character.HumanoidRootPart.Position).Magnitude
						print(Distance)
						print(PlayerDistance)
						if Distance < ShortestDistance then
							ShortestDistance = Distance
						end 
					end
				end
				wait()
			until true
		end)
		
		if Distance <= 30 and NearestPlayer.Character.Humanoid.Health ~= 0 then
			while PlayerDistance == nil do wait() end 
			path:ComputeAsync(script.Parent.HumanoidRootPart.Position, NearestPlayer.Character.HumanoidRootPart.Position)	
			Waypoints = path:GetWaypoints()
			for i, waypoint in pairs(Waypoints) do
				
				if Distance >= 30 and NearestPlayer.Character.Humanoid.Health == 0 then break end
				if PlayerDistance <= 6 then end
				if (waypoint.Position - NearestPlayer.Character.HumanoidRootPart.Position).Magnitude <= 6 then break end
				
				if waypoint.Action == Enum.PathWaypointAction.Jump then
					Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
				end
				
				Humanoid:MoveTo(waypoint.Position)
			end
		end
	until Distance >= 30 or NearestPlayer.Character.Humanoid.Health == 0

	path:ComputeAsync(script.Parent.HumanoidRootPart.Position, SpawnPoint.Position)	-- Go back to Spawn
	local Waypoints = path:GetWaypoints()
	for i, waypoint in pairs(Waypoints) do
		if waypoint.Action == Enum.PathWaypointAction.Jump then
			Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			end
		Humanoid:MoveTo(waypoint.Position)
	end
end
2 Likes

I believe the issue is that Humanoid:MoveTo() doesn’t orient the NPC toward the player. You would need to adjust the HumanoidRootPart CFrame to face the player after each movement. You can implement this by calculating the direction vector towards the player and updating the CFrame accordingly.

3 Likes

Works now :grin:.
Is there anything that I can improve to make the Code look better?

1 Like

Yeah, there are a few things you could do.

Firstly, while loops aren’t considered to be very efficient and developers tend to use RunService to handle things you could with while loops.

Secondly, I would recommend using module scripts to keep the script organised and easy to use. Using an Object-Oriented approach would be even better but its slightly more advanced.

Finally, (this is a pretty advanced subject but its very important especially when working with other developers or sharing your code) you should use custom types and type checking to make your script easier to use and understand.

If you want to learn more on these topics I suggest you read these posts:

1 Like

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