Pathfinding Script not looping

so I got the script working and now the ai is moving, but for whatever reason it isn’t looping??? It wont print the “Yes” at the end of the script which means for some reason it isn’t continuing

Script:

local PathfindingService = game:GetService("PathfindingService")

local MAX_RETRIES = 5
local RETRY_COOLDOWN = 5


local model = script.Parent
local humanoid = model.Humanoid
local humanoidRootPart = model.HumanoidRootPart

local path = PathfindingService:CreatePath()
local reachedConnection
local pathBlockedConnection 

	local function walkTo(targetPosition, yieldable)

		local RETRY_NUM = 0 
		local success, errorMessage
		local YIELDING = false

		repeat
			RETRY_NUM += 1 -- add one retry
			success, errorMessage = pcall(path.ComputeAsync, path, humanoidRootPart.Position, targetPosition)
			if not success then -- if it fails, warn the message
				warn("Pathfind compute path error: "..errorMessage)
				task.wait(RETRY_COOLDOWN)
			end
		until success == true or RETRY_NUM > MAX_RETRIES

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

				if not reachedConnection then
					reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
						if reached and currentWaypointIndex < #waypoints then
							currentWaypointIndex += 1

							humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
							if waypoints[currentWaypointIndex].Action == Enum.PathWaypointAction.Jump then
								humanoid.Jump = true
							end
						else
							reachedConnection:Disconnect()
							reachedConnection = nil
						end
					end)
				end

				pathBlockedConnection = path.Blocked:Connect(function(waypointNumber)
					if waypointNumber > currentWaypointIndex then
						reachedConnection:Disconnect()
						pathBlockedConnection:Disconnect()
						reachedConnection = nil
						pathBlockedConnection = nil
						YIELDING = false
						walkTo(workspace.Point2.Position, true)
					end
				end)

				humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
				if waypoints[currentWaypointIndex].Action == Enum.PathWaypointAction.Jump then
					humanoid.Jump = true
				end
				if yieldable then
					YIELDING = true
					repeat 
						task.wait()
					until YIELDING == false
				end
			end


		else-- if retry chance is maxed out
			warn("Pathfind compute retry maxed out, error: "..errorMessage)
			return
		end


	end
	
while true do
	
	task.wait(5)

	local MindWaypoints = workspace.MindsWaypoint:GetChildren()
	local ChosenWaypoint = MindWaypoints[math.random(1, #MindWaypoints)]
	walkTo(ChosenWaypoint.Position, true)
	
	walkTo():Disconnect()
	print("yes")
end

1 Like

I believe it has to do with the repeat until YIELDING == false part.
Looking at your code, it only seems to cease yielding if path.Blocked fires, and waypointNumber > currentWaypointIndex.

Consider adding more places where YIELDING is set to false, to ensure that it doesn’t get stuck waiting.


(Also on line 90, namely walkTo():Disconnect(), the code will error once you resolve the yielding issue, as Disconnect is not a valid function of walkTo().)

2 Likes

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