Pathfinding Zombie stuck in a jump loop

Everytime my pathfinding zombie is following me and it has to jump over something it gets stuck in a jump loop, constantly going back and forth while jumping. Is there a fix to this?

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

local Zombie = script.Parent
local Humanoid = Zombie:WaitForChild("Humanoid")
local RootPart0 = Zombie:WaitForChild("HumanoidRootPart")
RootPart0:SetNetworkOwner(nil)

local function GetRandomPlayer()
	for _, plr in pairs(Players:GetPlayers()) do
		local Character = plr.Character
		repeat task.wait() until Character
		local RootPart1 = Character:WaitForChild("HumanoidRootPart")
		return RootPart1
	end
end

local function CalculatePath(Destination)
	local Path = PathFindingService:CreatePath()
	Path:ComputeAsync(RootPart0.Position, Destination.Position)
	return Path
end

local function MoveToPath(Path)
	for _, Waypoint in pairs(Path:GetWaypoints()) do
		Humanoid:MoveTo(Waypoint.Position)
		if Waypoint.Action == Enum.PathWaypointAction.Jump then
			Humanoid.Jump = true
		end
		Humanoid.MoveToFinished:Wait()
	end
end

RunService.Heartbeat:Connect(function()
	local RootPart1 = GetRandomPlayer()
	if RootPart1 then
		local Path = CalculatePath(RootPart1)
		MoveToPath(Path)
	end
end)

The reason is due to having Humanoid.MoveToFinished:Wait() as new way points are quickly being generated due to runservice and your yielding it so remove that line

oh it works now. Thank you so much.

i have a lot of experience with pathfinding so i understand the problem

it isn’t stuck in a jump loop now, but by any chance do you know how to fix it where the zombie should drop down instead of jumping off when it gets on top of that obstacle?

Try putting humanoid:MoveTo() after the npc performs the jump instead of before

the same thing still happens and I just realized after watching the video that the zombie constantly jumps before reaching that obstacle i think its because since theres no wait() it jumps because it knows that one of the waypoints have an action equal to jump.

let me look at one of my pathfinding scripts

oh nevermind i fixed it all. I just made it check if the zombie was close enough to the waypoint with an action equal to jump before jumping. if you have a more efficient method please let me know, but thank you for the help.

if Waypoint.Action == Enum.PathWaypointAction.Jump and (RootPart0.Position - Waypoint.Position).Magnitude < 4 then
Humanoid.Jump = true
end

ok cool had that in my pathfinding script as well

1 Like

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