Help with Robot AI pathfinding

So basically I have this Robot AI that works pretty well so far except for the fact that it has one major problem. Whenever it locks onto a target and starts following them, it makes a path and follows that path. However, my robot is only following that path until it reaches the end of it even if the target has already moved somewhere else. How can I make it so that the robot breaks from its path once the target moves and then follows the new path. (I also don’t want the robot to stop while breaking from original path and generating a new one. I want the movement to be seamless)

Here is a video of the problem:

Here is the code snippet that moves the robot model:

local function makePath(root, destination)
	local pathParams = {
		["AgentHeight"] = 6.25,
		["AgentRadius"] = 4.5,
		["AgentCanJump"] = false
	}
	local path = pathfindingService:CreatePath(pathParams)
	path:ComputeAsync(root.Position, destination)
	
	workspace.Waypoints:ClearAllChildren()
	for _, waypoint in pairs(path:GetWaypoints()) do
		makeVisualizer(waypoint.Position)
	end
	
	return path
end

local function walkTo(humanoid, root, destination, hasTarget, target)
	local path = makePath(root, destination)
	
	if path.Status == Enum.PathStatus.Success then
		for _, waypoint in pairs(path:GetWaypoints()) do
			humanoid:MoveTo(waypoint.Position)
			humanoid.MoveToFinished:Wait(0.1)
			if hasTarget == true then
				print("Following target...")
			end
		end
	else
		print("Path failed")
		getUnstuck(root)
		task.wait(0.25)
	end
end

Any help would be greatly appreciated

Could you send the entire code?

actually I am going to lock this post bc I just found a solution

guess I can’t lock it. It won’t let me for some reason

Other players may be having a similar problem to you. Perhapse post your solution then mark it as the solution?

1 Like

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