Help on Pathfinding

  1. So, I was recently coding and I wanted to know how this code works? Why does the “nextWaypointIndex = 2” need to be put outside the reached Connection loop but not inside it.

  1. I found that reachedConnection is nil, so not nil should be true. and when it reaches the “reached and nextWaypointIndex < #waypoints”, I’m sure that it loops adds 1 to the waypointindex and ask the humanoid to move there. But, after that, I’m more confused, why put the “humanoid:MoveTo(waypoints[nextWaypointIndex].Position)” 2 times, inside and outside, because it doesn’t make sense.
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local path = PathfindingService:CreatePath()

local character = script.Parent --dummy model
local humanoid = character:WaitForChild("Humanoid")

local player = Players:FindFirstChild("towelmaster12")
local character = player.Character
local TEST_DESTINATION = character.Position

local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection

print(reachedConnection) --nil

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 --true
			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

followPath(TEST_DESTINATION)
  1. The pathfinder adds a node on where the character is located. The index is set to 2 instead of one, as a way to ignore the first node.

  2. I see no image.

reachedConnection is initially empty until followPath() is fired, in which the code snippet above will filled that in.

The humanoid:MoveTo(waypoints[nextWaypointIndex].Position) is called twice. The first time it is called, it is in followPath(), not inside the function connected to MoveToFinished. This is done only once so that the Humanoid can proceed walking to the next point, in which MoveToFinished will fire once complete. This starts the reachedConnection loop.

The other time it is called is inside the reachedConnection, in which it will only be fired once the Humanoid reached the next point.

2 Likes

Thank tou, @Y_VRN. You’re a lifesaver. I didn’t know who to ask about this issue.

1 Like

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