AI/pathfinding not jumping when required

I am currently working on an AI that chases the closest player. If it can see the player with a ray, it doesn’t pathfinding. If it cant see the player (which is when it would need to jump), it pathfinds.

My issue: The AI does not jump and just walks into the obstacle until it eventually gives up (for much longer than it should be, my MoveToFinished:Wait is set to 2 seconds, yet it still waks into the wall for around 10 seconds), or ends up jumping.

The part of the code that is giving me issues:

while wait() do
	local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
	
	if target then
		
		--ray to see if straight line, if not, pathfind
		local ray = Ray.new(AI.HumanoidRootPart.Position, (target.Position - AI.HumanoidRootPart.Position).Unit * 200)
		local hit, position = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})
		if hit then
			if hit:IsDescendantOf(target.Parent) then
				AI.Humanoid:MoveTo(target.position)
			else --pathfinding part!!!!!
				AI.Humanoid:MoveTo(target.Position)
				path = pfs:FindPathAsync(AI.HumanoidRootPart.Position, target.Position)
				points = path:GetWaypoints()
				if path.Status == Enum.PathStatus.Success then
					for i,v in pairs(points) do
						AI.Humanoid:MoveTo(v.Position)
						AI.Humanoid.MoveToFinished:Wait(2)
						if v.Action == Enum.PathWaypointAction.Jump then
							AI.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
						end
						if (points[#points].Position - target.Position).magnitude > 15 then
							break
						end
					end
				end
			end
		end
	end
end
1 Like

Maybe turn the humanoid’s autojump enabled?

1 Like

Currently, this property only works when the following conditions are true:

  • The Humanoid’s character model is the Player.Character of a Player .
  • The Player in question is using touch controls

Hmm I understand. Can you put a print inside this if statement? To see if this branch is ever taken?

1 Like

I put a print statement inside, and it still walked into the wall for a solid 10 seconds before printing “Jumping” and jumping. It is using the if statement, but it is taking forever to jump, and because of that the whole script gets stalled.

Maybe put the if before the MoveTo?

for i,v in pairs(points) do		
	if v.Action == Enum.PathWaypointAction.Jump then
		AI.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
    end
    AI.Humanoid:MoveTo(v.Position)
    AI.Humanoid.MoveToFinished:Wait(2)
	if (points[#points].Position - target.Position).magnitude > 15 then
		break
	end
end

EDIT: I suspect v.Position is a point that needs jumping to reach but you don’t check until after you told the AI to move to it’s position.

2 Likes

It was moving it before the :MoveTo that helped
Thankyou!

1 Like