Pathfind jumping infinitly

I’m trying make a PathFind service jump, but when the NPC will jump he just jump infinitely.

Video Script:


local module = {}

function module.Follow(Humanoid,HRP)

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

	local path = PathService:CreatePath(
		{
			AgentCanJump = true;
		}
	)
	local WayPoint, NextWayPoint
	local ReachedConnection

	local Sets = script.Settings
	local MaxDistance = Sets.WhereStop.Value

	for _, Player in Players:GetPlayers() do
		local Chr = Player.Character or Player.CharacterAdded:Wait()
		local Target = Chr:FindFirstChild("HumanoidRootPart")

		if Target then

			local DistanceFromBoth = (Target.Position - HRP.Position).Magnitude

			if DistanceFromBoth <= MaxDistance then

				local OriginalTarget = Target.CFrame * CFrame.new(2,0,5)

				local sucess = pcall(function()
					path:ComputeAsync(HRP.Position, OriginalTarget.Position)
				end)

				if sucess then
					WayPoint = path:GetWaypoints()
					if #WayPoint == 0 then return end

					if not ReachedConnection then
						ReachedConnection = Humanoid.MoveToFinished:Connect(function(reached)
							if reached and NextWayPoint > #WayPoint then
								NextWayPoint += 1
								Humanoid:MoveTo(WayPoint[NextWayPoint].Position)
								
							end
						end)
						NextWayPoint = 2
					end
					
					for i, Point in ipairs(WayPoint) do
						Humanoid:MoveTo(WayPoint[NextWayPoint].Position)
						if Point.Action == Enum.PathWaypointAction.Jump then
							Humanoid.Jump = true
						end
					end

					end
				end
			end	
		end
	end

return module

1 Like