Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help with pathfinding?

The script creates one node each time when it should be creating more then one why and how do I fix it?

Even though the script fails to visualize the path the Character still moves.

Pathfinding = game:GetService("PathfindingService")
Character = game.Workspace.Character.Humanoid
Start = Character.Torso.Position
End = Vector3.new(10,10,10)
---------------------------
function FindPointModel()
	local FoundPointModel = game.Workspace:FindFirstChild("PointModel")
		if not FoundPointModel then
			local Model = Instance.new("Model",game.Workspace)
			Model.Name = "PointModel"
		else return FoundPointModel--If found then return the Model
	end
end
---------------------------
function VisualizePath(Path)
	local Points = Path:GetPointCoordinates()
	for _,Point in pairs(Points) do
		print("Given Points are : ",Point)
		local Part = Instance.new("Part",FindPointModel())--Creates Nodes
			Part.Position = Point
			Part.Name = "Nodes"
			Part.Anchored = true
			Part.Size = Vector3.new(1,1,1)
			Part.CanCollide = false
			return Points --Returns Vairble Points:Path:GetPointCoordinates()
	end
end
---------------------------
Path = Pathfinding:ComputeSmoothPathAsync(Start,End,512)
Points = VisualizePath(Path)
for i,v in pairs (Points) do
	Character:MoveTo(v)
end

1 answer

✓ Accepted answer

Why are you returning your Points inside of the for loop? I would also recommend you to name your parts by id and if you are going to do this more than once use :ClearAllChildren()on the PointModel when you call VisualizePath.

Code I ended up with:

function VisualizePath(Path)
    local Points = Path:GetPointCoordinates()
    for i,Point in pairs(Points) do
        print("Given Points are : ",Point)
        local Part = Instance.new("Part",FindPointModel())--Creates Nodes
            Part.CFrame = CFrame.new(Point) -- I would use this just in case the Path goes through a wall.
            Part.Name = "Node"..i
            Part.Anchored = true
            Part.Size = Vector3.new(1,1,1)
            Part.CanCollide = false
             --return Points DON'T DO THIS, it will break the loop on the first point. That's also why the character still moves to the points.
    end
	return Points
end

Just replace the VisualizePath function.

If I helped out click the Accept Answer button and press that up arrow on the right. It really helps me both in the long and short run.

This discussion is preserved as it was. New answers, comments, and votes are closed.