Pathfinding won't jump down

My NPC Pathfinding won’t jump down a tall block.

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

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local WalkSpeed = humanoid.WalkSpeed

local DestinationsFolder = workspace:WaitForChild("Destinations")

local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection
local foundNextWaypoint = false

local currentPath = nil
local loopConnetion

local function randomWaypoint()
	local waypoint = DestinationsFolder:GetChildren()
	local random = waypoint[math.random(1, #waypoint)]
	return random
end

local function WalkTo(destination)
	local pathParams = {
		AgentHeight = character:GetExtentsSize().Y,
		AgentRadius = 2,
		AgentCanJump = false,
		AgentCanClimb = true,
		Costs = {
			Danger = math.huge,
			Climb = 2,
		}
	}

	local path = PathfindingService:CreatePath(pathParams)

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

	waypoints = {}
	
	blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
		if blockedWaypointIndex >= nextWaypointIndex then
			blockedConnection:Disconnect()
			WalkTo(destination)
		end
	end)

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

		if not reachedConnection then
			reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
				if reached and nextWaypointIndex < #waypoints then
					nextWaypointIndex += 1
					humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
					foundNextWaypoint = true
				else
					currentPath = nil
					foundNextWaypoint = false
					WalkTo(randomWaypoint())
				end
			end)
		end
		
		if waypoints[nextWaypointIndex] and waypoints[nextWaypointIndex].Action == Enum.PathWaypointAction.Jump then
			humanoid.Jump = true
		end

		if foundNextWaypoint == false then
			nextWaypointIndex = 2
			humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
		else
			nextWaypointIndex = 2
		end
	else
		humanoid:MoveTo(destination.Position)
	end
end

loopConnetion = RunService.Heartbeat:Connect(function()
	if currentPath == nil then
		WalkTo(randomWaypoint())
		currentPath = randomWaypoint()
	else
		WalkTo(currentPath)
	end

	if humanoid.Health <= 0 and loopConnetion then
		loopConnetion:Disconnect()
	end
end)

In the script you have pasted there is a humanoid:MoveTo(destination.Position) in the else block of the if success and path.Status == Enum.PathStatus.Success statement, this makes it so that it walks straight towards the destination position if it cannot find a way to reach it, clearly the issue is that it fails to find a way to reach it and does not move, did you perhaps comment this line out and then accidentally post an old version of the script? This verson of the script works for me.

Ohh, I removed that line because if the path is blocked, the pathfind would walk into walls trying to get to the destination.

Oh. I actually solved it!

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

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local WalkSpeed = humanoid.WalkSpeed

local DestinationsFolder = workspace:WaitForChild("Destinations")

local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection
local foundNextWaypoint = false

local currentPath = nil
local oldDestination = nil
local loopConnetion

local function randomWaypoint()
	local waypoint = DestinationsFolder:GetChildren()
	local random = waypoint[math.random(1, #waypoint)]
	return random
end

local function WalkTo(destination)
	local pathParams = {
		AgentHeight = character:GetExtentsSize().Y,
		AgentRadius = 2,
		AgentCanJump = true,
		AgentCanClimb = true,
		Costs = {
			Danger = math.huge,
			Climb = 2,
		}
	}

	local path = PathfindingService:CreatePath(pathParams)

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

	waypoints = {}

	blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
		if blockedWaypointIndex >= nextWaypointIndex then
			blockedConnection:Disconnect()
			WalkTo(destination)
		end
	end)

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

		if not reachedConnection then
			reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
				if reached and nextWaypointIndex < #waypoints then
					nextWaypointIndex += 1
					humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
					foundNextWaypoint = true
				else
					WalkTo(randomWaypoint())
					currentPath = nil
					foundNextWaypoint = false
				end
			end)
		end
		
		if waypoints[nextWaypointIndex] and waypoints[nextWaypointIndex].Action == Enum.PathWaypointAction.Jump then
			humanoid.Jump = true
		end

		if foundNextWaypoint == false then
			nextWaypointIndex = 2
			humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
		else
			nextWaypointIndex = 2
		end

	elseif not success and path.Status ~= Enum.PathStatus.Success then
		humanoid:MoveTo(destination.Position)
	end
end

loopConnetion = RunService.Heartbeat:Connect(function()
	if currentPath == nil then
		local random = randomWaypoint()
		
		if oldDestination ~= random then
			WalkTo(random)
			currentPath = random
			oldDestination = random
		end
	else
		WalkTo(currentPath)
	end

	if humanoid.Health <= 0 and loopConnetion then
		loopConnetion:Disconnect()
	end
end)
1 Like

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