How do I break a Humanoid:MoveTo() function?

Hello, I am a bit of a newcomer to complex scripting using LuaU. I am creating an AI system for an NPC in one of my games, however I cannot for the life of me figure out how to break this function. I’ve used return and cannot think of any other ways to conclude a Humanoid:MoveTo() sequence early. Any help is appreciated.

local pathFind = game:GetService("PathfindingService")
local RunService = game:GetService("RunService")

local test_destination = nil

local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection

local char = script.Parent
local hum = char:WaitForChild("Humanoid")

local path = pathFind:CreatePath({
	AgentRadius = 3,
	AgentHeight = 6,
	AgentCanJump = true,
	AgentCanClimb = true,
	Costs = {
		Water = 20
	}
})



local function followPath(destination, id)
	local success, errorMessage = pcall(function()
		path:ComputeAsync(char.PrimaryPart.Position,destination)
	end)
	
	if success and path.Status == Enum.PathStatus.Success then
		waypoints = path:GetWaypoints()
		
		blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
			if blockedWaypointIndex >= nextWaypointIndex then
				blockedConnection:Disconnect()
				followPath(destination)
			end
		end)
		if not reachedConnection then
			reachedConnection = hum.MoveToFinished:Connect(function(reached)
				if reached and nextWaypointIndex < #waypoints then
					nextWaypointIndex += 1
					hum:MoveTo(waypoints[nextWaypointIndex].Position)
				else
					reachedConnection:Disconnect()
					blockedConnection:Disconnect()
				end
			end)
		end
		nextWaypointIndex = 2
		if id == script.ID.Value then
			hum:MoveTo(waypoints[nextWaypointIndex].Position)
		else
			return
		end
	else
		warn("Path not computed!", errorMessage)
	end
end

while task.wait(1) do
	repeat task.wait(0.01) until
game.Workspace.Players:FindFirstChildWhichIsA("Model") -- this is a premade folder put into workspace that finds the first model
	test_destination = Vector3.new(game.Workspace.Players:FindFirstChildWhichIsA("Model").HumanoidRootPart.Position)
	print(test_destination)
	script.ID.Value += 1
	followPath(test_destination, script.ID.Value)
end

Update, I’ve seen what the issue may ACTUALLY be; when I used the print(test_destination) line, I came across a discovery. Instead of printing the value of the Vector3 at that exact point, it instead relies back on the origin of the entire baseplate. I will see if removing the Vector3.new() tidbit causes it to work again.

Update 2: never use Vector3.new unless you absolutely have to. The solution here was to remove Vector3.new() entirely.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.