Stop Pathfinding Getting Stuck

my system has 3 scripts

botsciptmain

local PFS = game:GetService("PathfindingService")

local Bot = script.Parent
local RP = Bot.HumanoidRootPart
local Human = Bot:FindFirstChild("Humanoid")



local Path = PFS:CreatePath({
	AgentRadius = 1,
	AgentHeight = 6,
	AgentCanJump = false,})


while true do
	local LD = math.huge
	local HPart 
	for i, player in pairs(game.Players:GetPlayers()) do
		if player.Character:FindFirstChild("HumanoidRootPart") then
			local mag = (player.Character:FindFirstChild("HumanoidRootPart").Position - RP.Position).Magnitude
			if mag < LD then
				LD = mag
				HPart = player.Character:FindFirstChild("HumanoidRootPart")
			end
		end
	end

	if HPart then
		Path:ComputeAsync(RP.Position,HPart.Position)
		if Path and Path.Status == Enum.PathStatus.Success then
				if #Path:GetWaypoints() > 4 then
					local WPs = Path:GetWaypoints()
					Human:MoveTo(WPs[4].Position)
				else
					Human:MoveTo(HPart.Position)

				end
		end
	end
end

spotscript

local Bot = script.Parent.Parent
local RP = Bot.HumanoidRootPart
local Human = Bot:FindFirstChild("Humanoid")
local Ambience = RP.Ambience
Ambience:Play()

local SpotedDistance = 25

local OrgVolume = 0.1
local OrgSpeed = 0.8
local ORGDIST = 55



while true do
	for a, Player in pairs(game.Players:GetPlayers()) do
		if Player and Player.Character then
			local Root = Player.Character:FindFirstChild("HumanoidRootPart")
			local mag = (Root.Position - RP.Position).Magnitude



			if mag < SpotedDistance  then
				Ambience.Volume = 0.25
				Ambience.PlaybackSpeed = 1
				Ambience.RollOffMaxDistance = 70
				RP.Spotted:Play()
				script.Parent.Enabled = true
				script.Parent.WanderWaypoints.Enabled = false
				Human.WalkSpeed = Human.WalkSpeed * 2
				--game.ReplicatedStorage.Spotted:FireAllClients(Bot)
				task.wait(5)
				Human.WalkSpeed = Human.WalkSpeed / 2
				script.Parent.Enabled = false
				script.Parent.WanderWaypoints.Enabled = true
				Ambience.Volume = OrgVolume
				Ambience.PlaybackSpeed = OrgSpeed
				Ambience.RollOffMaxDistance = ORGDIST
			end
		end
	end
	task.wait(0.1)
end

AND wanderwaypoints:

for a,b in ipairs(script.WAYPOINTS_FOLDER.Value.InCooldownWaypoints:GetChildren()) do
	b.Parent = script.WAYPOINTS_FOLDER.Value.WanderPoints
end



local Bot = script.Parent.Parent
local RP = Bot:WaitForChild("HumanoidRootPart")
RP:SetNetworkOwner(nil)
local Human = Bot:WaitForChild("Humanoid")



-- Function to wait for the humanoid to reach its destination
local function waitForMove()
	local moveFinished = false
	local connection

	connection = Human.MoveToFinished:Connect(function(reached)
		moveFinished = true
		if connection then
			connection:Disconnect()
		end
	end)

	-- Wait until the move is finished or a timeout occurs
	local startTime = tick()
	while not moveFinished do
		if tick() - startTime > 10 then  -- 10 second timeout
			print("Move timed out")
			break
		end
		task.wait()
	end
end

-- Function to follow the waypoints
local function followPath(waypoints)
	for _, waypoint in ipairs(waypoints) do
		Human:MoveTo(waypoint.Position)
		waitForMove()
	end
end

-- Function to handle pathfinding and movement
local function moveToWaypoint(part)
	local Path = game.PathfindingService:CreatePath({
		AgentRadius = 1,
		AgentHeight = 6,
		AgentCanJump = false,
		AgentMaxSlope = 80,  -- Adjust based on your environment
		AgentCanClimb = false,  -- Adjust based on your environment
	})
	Path:ComputeAsync(RP.Position, part.Position)

	if Path.Status == Enum.PathStatus.Success then
		local WPs = Path:GetWaypoints()
		if #WPs > 0 then
			followPath(WPs)  -- Follow the waypoints
		end
	else
		print("Pathfinding failed with status:", Path.Status.Name)
	end
end

-- Main loop
while true do
	local Folder = script.WAYPOINTS_FOLDER.Value.WanderPoints:GetChildren()
	if #Folder >= 1 then
		local Rnum = math.random(1, #Folder)
		local Part = Folder[Rnum]

		task.spawn(function()
			Part.Parent = script.WAYPOINTS_FOLDER.Value.InCooldownWaypoints
			wait(10)
			Part.Parent = script.WAYPOINTS_FOLDER.Value.WanderPoints
		end)

		moveToWaypoint(Part)
	else
		print("No waypoints available")
	end

	task.wait(0.01)  -- Short wait before starting the next path
end

they are placed like this:
image

and the purpose of the script is to make a bot walk through the random waypoints and if a player gets close it starts chasing and after 5 seconds it goes back to waypoint wandering.

if anyone has a better way of doing this, id appreciate feedback.

PROBLEM: the bot always gets stuck 2 seconds after going from chasing back to the waypoint system.


green: bot
red: waypoints
yellow: where it commonly gets stuck

image

it gets stuck just like this