Cloning a path created with PathfindingService:CreatePath() returns nil

Hello guys,
I am trying to clone a path object:

PathfindingService:CreatePath()

and when I use:

path:Clone()

it doesn’t work and returns nil. By the way the path exists I checked it
and I set archivable to true

sorry for bumping but I still haven’t found the solution

The script:

print(path) -- returns Instance
path.Archivable = true
local lpath = path:Clone()
print(lpath) -- returns nil

PathfindingService:CreatePath() returns a path object. This object cannot be cloned in the standard sense, from what I remember at least. Why are you trying to clone the path?

I need a copy so I can compute multiple paths with one pathobject

So you can’t clone a path object like that. Path objects are for one specific path only. You can re-compute the path if you need the path to dynamically update, but if you’re trying to create multiple paths, you will need multiple Path objects.

If you have already run Path:ComputeAsync(), and you want to just clone the waypoints as a permanent path, you can do local waypoints = Path:GetWaypoints() and iterate through that, making a new part for each waypoint.

I should note, I don’t really know why you are trying to compute multiple paths. Are you trying to dynamically update one path? Are you trying to create a permanent path for an NPC to always follow that one specific goal?

I am making a dynamic monster that chases the player, but I don’t want to create a new path everytime I want to compute a path and the pathfinding should run async. But I can’t just clone the waypoints

local function pathfindasync(objekt)
	task.spawn(function()
		path.Archivable = true
		local lpath = path:Clone()
		local targetposition = objekt.Position
		local rayparams = RaycastParams.new()
		rayparams.FilterType = Enum.RaycastFilterType.Exclude
		rayparams.FilterDescendantsInstances = {objekt.Parent}
		local viewcheck = workspace:Blockcast(monster.PrimaryPart.CFrame * CFrame.new(0,0.1,0), monster:GetExtentsSize() * 0.9, getonlyxz(objekt.Position) - getonlyxz(monster.PrimaryPart.Position),rayparams)
		if not viewcheck then
			lastpathfindposition = targetposition
			table.clear(waypoints)
			if enabled.Value then
				humanoid:MoveTo(objekt.Position)
			end
		else
			if math.abs(targetposition.Magnitude - lastpathfindposition.Magnitude) ~= 0 then
				local success, errorMessage = pcall(function()
					lpath:ComputeAsync(monster.PrimaryPart.Position,objekt.Position)
				end)
				if success and lpath.Status == Enum.PathStatus.Success then
					lastpathfindposition = targetposition
					if enabled.Value then
						waypointindex = 2
						waypoints = lpath:GetWaypoints()
						if not pathfinding then
							pathfinding = true
							blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
								if blockedWaypointIndex >= waypointindex then
									blockedConnection:Disconnect()
									reachedConnection:Dissconnect()
									pathfinding = false
									blocked = true
								end
							end)
							reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
								if reached and waypointindex < #waypoints and enabled.Value then
									waypointindex += 1
									humanoid:MoveTo(waypoints[math.min(#waypoints,waypointindex)].Position)
									waypoints[math.min(#waypoints,waypointindex)] = nil
								else
									reachedConnection:Disconnect()
									blockedConnection:Disconnect()
									pathfinding = false
								end
							end)
						end
						humanoid:MoveTo(waypoints[math.min(#waypoints,waypointindex)].Position)
					end
				end
			end
		end
	end)
end

this is run in a while loop nearly every tick

The major issue I see here is that you are trying to clone the path object. Just have the NPC move a couple waypoints up, and then recompute the path and get the waypoints again.

If you’re confused how to recompute a path, it is as simple as running path:ComputeAsync() on it again. You do need to run path:GetWaypoints() again after. You do not need a new path instance for every path update.

But I want to compute the last path while the new one is computed, I will just create a new path instance everytime

You will need to call PathfindingService:CreatePath several times then.

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