Horror Pathfinding AI Stuck

Hello,

I am having trouble with this pathfinding system. It’s created by GnomeCode and I’ve been using it for quite a while. I recently noticed that the AIs are getting stuck very often. The code has a teleportation script where if the AI gets stuck for a certain amount of time, it teleports the AI back to its original spot. Whenever it teleports back, the AI keeps taking the path it took originally. Resulting in this:

This is the code that may have to do with it:

local function blockToBlock()
	if path and not reached then
		coroutine.wrap(function()
			path.Reached:Wait()
			reached2 = true
		end)()
		repeat wait() if path == nil then break end until reached2 == true
	end
	reached = false
	reached2 = false
	path = nil
	coroutine.wrap(function()
		wait(1)
		if followingTarget and chasing == false then
			for i, v in pairs(game.Players:GetPlayers()) do
				if v.GettingChasedBy.Value == script.Parent then
					v.GettingChased.Value = false
					v.GettingChasedBy.Value = nil
				end
			end
			chasing = false
			followingTarget = nil
		end
	end)()
	local goal = workspace.LoopPoints:GetChildren()[Random.new():NextInteger(1,#workspace.LoopPoints:GetChildren())]
	local path = getPath(goal)
	if path.Status == Enum.PathStatus.Success then
		--displayPath(path:GetWaypoints())
		for i, v in pairs(path:GetWaypoints()) do
			if findPotentialTarget() then
				if canSeeTarget(findPotentialTarget().Character) then
					break
				end
			end
			TeddyAI.Humanoid:MoveTo(v.Position)
			TeddyAI.Humanoid.MoveToFinished:Wait()
		end
	else
	end
end
2 Likes

Try adding this:

for i, v in pairs(path:GetWaypoints()) do
    if findPotentialTarget() then
    	if canSeeTarget(findPotentialTarget().Character) then
    		break
    	end
    end
    TeddyAI.Humanoid:MoveTo(v.Position)
    local check = TeddyAI.Humanoid.MoveToFinished:Wait(1)
    
      if not check then
            blockToBlock()
            break
      end
end

This has a check that utilizes the MoveToFinished:Wait() function, if the AI cannot reach the waypoint (if it’s stuck), it will break the waypoint loop and recall the function, practically recalculating it’s path.

Just tested and it worked. Hopefully it really works in-game.