Pathfinding script won't let the NPC jump

I have this code that I wrote, for some reason, the NPC won’t jump even though the “AgentCanJump” is set to true. I do not know why that is, and what I need to fix it.

I want to add jumping to jump over obstacles that the NPC may come across.

I tried looking at a tutorial that showed how to add jumping to pathfinders, but that didn’t work.
Tutorial link: Here

Script:

-- Variables --

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

local path = Pathfinding:CreatePath({
	AgentHeight = 6,
	AgentRadius = 3,
	AgentCanJump = true,
	
	Costs = {
		Water = 100;
		DangerZone = math.huge
	}
})

local Character = script.Parent
local humanoid = Character:WaitForChild("Zombanoid")

local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection

-- Functions --

local function findTarget()
	local maxdistance = math.huge
	local nearestTarget
	
	for index, player in pairs(Players:GetPlayers()) do
		if player.Character then
			local target = player.Character
			local distance = (Character.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
			
			if distance < maxdistance and target.Humanoid.Health > 0 then
				nearestTarget = target
				maxdistance = distance
				
			end
		end
	end
	
	return nearestTarget
end

local function followpath(destination)
	
	local success, errorMessage = pcall(function()
		path:ComputeAsync(Character.PrimaryPart.Position, destination)
	end)
	
	if success and path.Status == Enum.PathStatus.Success then
		waypoints = path:GetWaypoints()

		blockedConnection = path.blocked:Connect(function(blockedWaypointIndex)
			if blockedWaypointIndex >= nextWaypointIndex then
				blockedConnection:Disconnect()
				followpath(destination)
			end
		end)
		
		if not reachedConnection then
			reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
				if reached and nextWaypointIndex < #waypoints then
					nextWaypointIndex += 1
					humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
				else
					reachedConnection:Disconnect()
					blockedConnection:Disconnect()
				end
			end)
		end
		
		nextWaypointIndex = 2
		humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
	else
		warn("No good, Path not computed.", errorMessage)
	end
end

-- Connections --

while wait() do
	local target = findTarget()
	if target then
		print(target.Name)
		followpath(target.HumanoidRootPart.Position)
	end
end

-- Other -- 

local npcRootPart = script.Parent.HumanoidRootPart
npcRootPart:SetNetworkOwner(nil)

Please help me. :frowning_face:

1 Like

PathWaypoint's have as well as having a Position property, have an Action property.
If Action == Enum.PathWaypointAction.Jump then set Humanoid.Jump to true

I know that one, but I tried it once and it didn’t work. I do not know how/where to implement it into the script.

I tried it like this

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

But that didn’t work, I think I am doing something wrong. :thinking:

In that script, if the action IS NOT jump, it will perform nothing. Lmk if the following works:

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

It did not work, I feel like I may have done something wrong in the script.