Can someone explain this part of the pathfinding script for me?

local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local path = PathfindingService:CreatePath()

local player = Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

local TEST_DESTINATION = Vector3.new(100, 0, 100)

local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection

local function followPath(destination)
	-- Compute the path
	local success, errorMessage = pcall(function()
		path:ComputeAsync(character.PrimaryPart.Position, destination)
	end)

	if success and path.Status == Enum.PathStatus.Success then
		-- Get the path waypoints
		waypoints = path:GetWaypoints()

		-- Detect if path becomes blocked
		blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
			-- Check if the obstacle is further down the path
			if blockedWaypointIndex >= nextWaypointIndex then
				-- Stop detecting path blockage until path is re-computed
				blockedConnection:Disconnect()
				-- Call function to re-compute new path
				followPath(destination)
			end
		end)

		-- Detect when movement to next waypoint is complete
		if not reachedConnection then
			reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
				if reached and nextWaypointIndex < #waypoints then
					-- Increase waypoint index and move to next waypoint
					nextWaypointIndex += 1
					humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
				else
					reachedConnection:Disconnect()
					blockedConnection:Disconnect()
				end
			end)
		end

		-- Initially move to second waypoint (first waypoint is path start; skip it)
		nextWaypointIndex = 2
		humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
	else
		warn("Path not computed!", errorMessage)
	end
end

This is the generic pathfinding script ripped straight from the character pathfinding page on creator documentation.
My full understanding of how the script works is pretty important for me using it, and a lot of it seems to make sense, except probably the main idea (which is kinda pathetic lol)

Basically, it seems to just be a bunch of if statements, without loops, without any real recursive functioning (besides recomputing a failed path). It seems like it’s just one movement that never continues anywhere? And then immediately after the if statement it seems to reset the waypoint to 2? So why doesn’t it go in circles?

My main problem seems to be understanding the connection between the reachedConnection function set up, so if I’m right in that being my issue, can someone explain it to me? And if I’m wrong, can someone explain what it actually is I’m not understanding?

Here’s why it works:

the reachedConnection is declared as

local reachedConnection

so reachedConnection equals nil

then it gets through the

if not reachedConnection then

reachedConnection is declared but will never get called

unless

humanoid:MoveTo(waypoints[nextWaypointIndex].Position)

is called

when humanoid finishes reaching to the desired position (waypoint),
reachedConnection is called

then it checks if it reached its final destination,

if didn’t reach final destination, then repeat the same whole process (like some type of loop)

if yes, then followPath(destination) ends.


So It does not go in loops or circles. reachedConnection is The Loop

Hope it helps! (at least in some way)

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