MoveTofinished:Wait() or :Wait() is broken

Hello everyone. I am making an AI, no further explanation needed. Obviously I used to .MoveToFinished:Wait() function. It is simply not working. Here is the code.

local function walkTo(destination)

	local path = getPath(destination)

	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(path:GetWaypoints()) do
					
			local target = findIntruder()
			
			if target then
				
				local chase = getChase()
				local counter = 0
				
				for i, sound in pairs (soundservice.Ambiences.ChaseMusics:GetChildren()) do

					if sound.Playing == false then

						counter += 1

					end

				end

				if counter == #soundservice.Ambiences.ChaseMusics:GetChildren() then

					chase.Playing = true

				end
				
				mode.Value = "Attack"
				
				local nearestroom = findNearestRoom(target)
				local tag = nearestroom:FindFirstChild("Favorite")
				
				if not tag then
					addFavLoc(nearestroom)
				end
				
			elseif not target then

				humanoid:MoveTo(waypoint.Position)
				print("move")
				humanoid.MoveToFinished:Wait()
				print("finish")
					
				end
		end
	else
		humanoid:MoveTo(destination - (hrp.CFrame.LookVector * 10)

		)
	end

The prints repeat over and over. There is no yield. I am almost certain that the .MoveToFinished:Wait() is broken, or the :Wait() is broken because this wont work and also I tried to use .Stopped:Wait() for an animation and it didn’t work. If I am doing something wrong, I would love to know if you have a solution.

If you’re just wanting the for loop to end, add a break in the for loop after the :Wait().

no the .MoveToFinished function is just not working for some reason I dont know why

Try doing

local reached = humanoid.MoveToFinished:Wait()
print("reached location:", reached)

You might get some information on that.

Also try these.

  1. Replace MoveToFinish:Wait() with task.wait(1), does the NPC still move?
  2. Try adding task.wait() right after :MoveTo() and see if it fixes it.
  3. Check your humanoid/character and make sure they can actually move. The MoveToFinished might be failing because the Humanoid can’t move.
  4. Check and make sure waypoint.Position isn’t == hrp.Position always
  5. Isolate your code and test it on a basic R6/R15 dummy rig without any other code messing with it.

I agree with treebee, add task.wait(), make sure your NPC can actually move, and make sure your script is the correct script and it is in the correct, meaning such as a server script should be in ServerScriptService, or a local script could be inside the StarterCharacterScripts.