Pathfinding Bug

hello i seem to have a bug with my pathfinding, for some reason when i occasionally make sharp turns, the ai seems to bug out and stop following me like in the video below vv

External Media

here is my code:

local function Patrol(monster, SpawnLocation) --[6] so with the monster MODEL, and the SPAWNLOCATION VECTOR(origin of patrol)
while true do
local PatrolPositions = GetRandomPatrolPoint(monster, SpawnLocation) – get patrol points

	if #PatrolPositions > 0 then
		local RandomDestination = PatrolPositions[math.random(1, #PatrolPositions)] -- pick a random patrol point
		local Path = GetPath(monster.PrimaryPart.Position, RandomDestination)

		if Path then
			for _, waypoint in ipairs(Path:GetWaypoints()) do	
				
				Path.Blocked:Connect(function()
					Path:Destroy()
				end)
				-- move to each waypoint in the path

				-- check for the target while moving
				local Target = FindTarget(monster)
				if Target then
						LastPos = Target.HumanoidRootPart.Position
						monster.Humanoid:MoveTo(LastPos)
					
						break
				else
					if LastPos then
						monster.Humanoid:MoveTo(LastPos)
						monster.Humanoid.MoveToFinished:Wait()
						LastPos = nil --resets 
						break
					else
						monster.Humanoid:MoveTo(waypoint.Position)
						local success = monster.Humanoid.MoveToFinished:Wait()
		
						
					end
					
					
				end
				
					


				
			end

			

		else
			warn("Failed to find path to " .. tostring(RandomDestination))
		end
	else
		warn("No valid patrol positions found")
	end


end

end

1 Like