Path Finding Bugging (Pathfinding Service)

heres the video of what i mean

local runService = game:GetService("RunService")
local players = game:GetService("Players")
local pathfindingService = game:GetService("PathfindingService")
local bots = workspace.bots

local path = pathfindingService:CreatePath({
	AgentRadius = 0.5, 
	AgentHeight = 4, 
	WaypointSpacing = 30, 
	AgentCanJump = true, 
	AgentCanClimb = true, 
	Costs = {
		Climb = 0.85, 
		Launchable = 1.01, 
		Jump = 10, 
		Hydrant = 20, 
		Travel = 1
	}
})

local function getNearestPlayer(from)
	local shortestDistance, shortestFrom = 999999999999, nil

	for _, player in players:GetPlayers() do
		if player.Character then
			local humanoidRootPart = player.Character.HumanoidRootPart
			local distance = (from - humanoidRootPart.Position).Magnitude

			if distance <= shortestDistance then
				shortestDistance = distance
				shortestFrom = player
			end
		end
	end

	return shortestFrom, shortestDistance
end

runService.Heartbeat:Connect(function(step)
	for _, bot in bots:GetChildren() do
		local player, distance = getNearestPlayer(bot.HumanoidRootPart.Position)

		if player then
			path:ComputeAsync(bot.HumanoidRootPart.Position, player.Character.HumanoidRootPart.Position)

			if path.Status == Enum.PathStatus.Success then
				local waypoints = path:GetWaypoints()

				for _, waypoint in waypoints do
					bot.Humanoid:MoveTo(waypoint.Position)
					bot.Humanoid.MoveToFinished:Wait()
				end
			end
		end
	end
end)

if i should guess im gussing its because of the runservice loop since it’d be creating so many paths but idk

Wait() doesnt work on heartbeat. Use while task.wait instead of heartbeat

Change:

To:

for i = 3, #waypoints do
					bot.Humanoid:MoveTo( waypoints[i].Position )	
				end

I have no idea how, but this fixed most of my pathfinding problems.
Also don’t add a wait to my script, that will break it again.

i’ve just tried both of those suggestions, the bot still bugs sort of. it moves to where i was when it started the path but not where i currently am

You should have the bot raycast to the player to see if there’s anything in the way, that way you can just have them move directly towards you, saving the cost of making a path, and also being quicker to reach you

1 Like

I see quite a few things you can change. I’ll list them here

  1. Set the shortest distance to a smaller number eg. 1000 instead of such a big one
  2. I don’t see the purpose of needing to return the distance from the ‘getNearestPlayer’ function. Just remove it and only return the nearest player
  3. Use a while loop for computing and targeting instead of runservice.Heartbeat()
while wait() do
   targetplayer() -- Replace this with your function for targeting 
end
  1. It would be prudent to add a failsafe if the path status fails. For example:
if path.Status == Enum.PathStatus.Success then
		local waypoints = path:GetWaypoints()

		for _, waypoint in waypoints do
			bot.Humanoid:MoveTo(waypoint.Position)
			bot.Humanoid.MoveToFinished:Wait()
else
    bot.Humanoid:MoveTo(workspace.Failsafe.Position) -- Here, failsafe is a part in case path fails, replace with your own instance
end

If none of these work, it is probably something to do with your model.