Switch path for pathfinding

I’m trying to make a system so that when I move a part the NPC pathfinds to it.

It goes to the original position instead of going to the new position

local PathFindingService = game:GetService("PathfindingService")
local human = script.Parent:WaitForChild("Humanoid")
local torse = script.Parent:WaitForChild("Torso")

local modifierVolume = game.Workspace.crosswalk

local modifier = Instance.new("PathfindingModifier", modifierVolume)
	modifier.Label = "All"


local agentParameters = {
	
	Costs = {
		All = math.random(1,100) = 1 and 100 or 1

	}
}

local path = PathFindingService:CreatePath(agentParameters)
path:ComputeAsync(torse.Position, game.Workspace.End.Position)
local waypoints = path:GetWaypoints()

path.Blocked:Connect(function()
	print("Path Blocked")
end)

wait(0.1)
for i, waypoint in pairs(waypoints) do
	if waypoint.Action == Enum.PathWaypointAction.Jump then
		human:ChangeState(Enum.HumanoidStateType.Jumping)	
	end
	
	
	human:MoveTo(waypoint.Position)
	human.MoveToFinished:Wait(1)
	
end

human:MoveTo(game.Workspace.End.Position)
wait()
script.Parent:Destroy()

How do I make it so it updates the path every time the End part is moved?

just make
game.Workspace.End.Position.Changed:Connect(function(val)
and pathfind script here ig
end)

i need it to update the path tho, also wouldn’t work because it creates an entirely separate path each time the position changes

I’m pretty sure you have to make a new path, you can’t make one path longer or shorter without calculating a new path. You should do Path:ComputeAsync(torse.Position, game.Workspace.End.Position) when the position of the end point is changed but before you do that you have to block the for loop that is currently making it walk the original path, you can do this with debounces.

2 Likes