Ipairs through waypoints

Hello, another problem found.

for i,v in ipairs(waypoints) do
hum:MoveTo(waypoints.Position)
end
--[[the waypoints is a table with parts. How do i choose them from oldest to recent? Like:
waypoints={
point <start
point
point
point
point <end
}
Choose from start point, to middle points, to end point.
]]

The issue you’re most likely running into is that you’re using GetChildren() to construct waypoints, which isn’t ordered. You can use the table.sort function to automatically sort the table’s contents.

A quick code snippet to help you:

table.sort(waypoints, function(part1, part2) -- This assumes each part has a number as its name
    local x, y = tonumber(part1.Name), tonumber(part2.Name)
    return x < y -- Sorts in an ascending order
end)

it would be helpful if you could send your entire code so i can see what you’re doing. It seems like you are a little confused on how for loops work. Right now, you’re npc likely won’t move at all because waypoints has no property “position”. Waypoints is a list of points (in order from closest point to farthest away). Here’s what you should try instead:
for _, waypoint in waypoints do
hum:MoveTo(waypoint.Position)
end

local plr=game:GetService("Players").LocalPlayer
local char=plr.Character or plr.CharacterAdded:Wait()
local hum=char.Humanoid
local root=char.HumanoidRootPart 


local service=game:GetService("PathfindingService")

local waypoints={}

local function AddWaypoint()
local point=Instance.new("Part", game.Workspace)
point.Position=root.Position
point.Anchored=true
point.Shape=Enum.PartType.Ball
point.Transparency=0.7
table.insert(waypoints, point)
end

local function Navigate()
local start=waypoints[1]
local end=waypoints[#waypoints]
local path=service:CreatePath()
path:ComputeAsync(start.Position, end.Position)
local ways=path:GetWaypoints()
for i,v in ipairs(waypoints) do
hum:MoveTo(waypoints.Position)
end
end

This should fix the issue:

local plr=game:GetService("Players").LocalPlayer
local char=plr.Character or plr.CharacterAdded:Wait()
local hum=char.Humanoid
local root=char.HumanoidRootPart 


local service=game:GetService("PathfindingService")

local waypoints={}

local function AddWaypoint()
	local point=Instance.new("Part", game.Workspace)
	point.Position=root.Position
	point.Anchored=true
	point.Shape=Enum.PartType.Ball
	point.Transparency=0.7
	table.insert(waypoints, point)
end

local function Navigate()
	local start=waypoints[1]
	local end=waypoints[#waypoints]
local path=service:CreatePath()
path:ComputeAsync(start.Position, end.Position)
	local ways=path:GetWaypoints()
for i,v in ipairs(ways) do -- ways is the variable that stores the waypoints returned by the path
	hum:MoveTo(v.Position) -- v is the variable that stores the waypoint
	hum.MoveToFinished:Wait() -- wait for the humanoid to reach the waypoint's position before moving to the next waypoint
end
end
1 Like

I changed the script, ill add the movetofinished, and ill try. I’ll see

1 Like

Thanks so much! I really appreciate your help.

1 Like

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