Pathfinding getting stuck in random places

Hey, so I’ve been working on this pathfinding A.I and it seems to be where whenever I start sprinting or just move too fast around him, he gets stuck and never moves ever again.

It doesn’t seem to do this error whenever I move around him normally.
Edit: Never mind. I walked right past him and he got stuck against a wall.

Here’s my code:

--Variables
local pathfindingService = game:GetService("PathfindingService")

--Dummy variables
local myHumanoid = script.Parent.Humanoid
local myTorso = script.Parent.HumanoidRootPart

--Configuration
local mag = script.Configuration.Magnitude
local view = script.Configuration.View
local check = script.Configuration.Check

--Variables
local foundPlayer

for _, Part in pairs(script.Parent:GetDescendants())do
	if Part:IsA("BasePart") then
		if Part:CanSetNetworkOwnership() then
			Part:SetNetworkOwner();
		end;
	end;
end;

--Function that finds the closest player
function findPlayer()
	local view = view.Value --View distance of dummy
	--Look in the workspace for players
	for i, v in pairs(game.Workspace:GetChildren()) do
		local humanoid = v:FindFirstChild("Humanoid")
		local torso = v:FindFirstChild("UpperTorso")
		if humanoid and humanoid.Health > 0 and torso and v.Name ~= script.Parent.Name then
			--Check if player is in view
			if(myTorso.Position - torso.Position).magnitude < view and humanoid.Health ~= 0 then
				foundPlayer = torso --Set them to the foundPlayer
			end
		end
	end
end

--Function that creates a path to player
function createPath(player)
	--Create the path
	local path = pathfindingService:CreatePath()
	path:ComputeAsync(myTorso.Position, player.Position)
	
	--Get the waypoints
	local waypoints = path:GetWaypoints()
	if path.Status == Enum.PathStatus.Success then
		--Loop through each waypoint
		for i, waypoint in pairs(waypoints) do
			--Make dummy jump if neeeded
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				myHumanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			end
			--Move to the closest waypoint
			myHumanoid:MoveTo(waypoint.Position)

			--Check if player moved
			if(myTorso.Position - waypoints[1].Position).magnitude > check.Value then
				createPath(player)
			end
			
			repeat 
				local distance = (waypoint.Position - myTorso.Position).magnitude
			wait() until distance <= 5
		end
	end
end

--Function that finds a player, and creates a path to them
function main()	
	findPlayer()
	createPath(foundPlayer)
end

--Run all of the functions above
while wait() do
	main()
end
1 Like

I’m not sure but try this…

See ‘Handling blocked paths’

1 Like

I tried that, but after awhile he got stuck in a wide open baseplate. There were no blocks or things in his way in at least a ten stud radius.

Edit: I found this DevForum post and it seems to have fixed the issue!

1 Like