local alien = script.Parent
local humanoid = alien:WaitForChild("Humanoid")
for i, v in pairs(workspace.GrasslandsDestinations:GetChildren()) do
humanoid:MoveTo(v.Position)
humanoid.MoveToFinished:Wait()
end
The order in the points is in the folder which is in the workspace is from least (top) to greatest (bottom). What I mean is that if the destination has a 1 in the name it is above the destination with a 3 in the explorer.
From Wiki:
â Remember, the â i â in ipairs() is for âindex.â Use ipairs() and arrays in combination with a for loop to work with ordered values like leaderboards.â
While this is true, ipairs wonât be helpful in this case, since the order of the values in table returned by GetChildren does not necessarily match the tree shown in the explorer.
In this case, @beedo1011 you could do this instead (just an example which can be improved, so you get the idea):
for i = 1, 6 do
local v = workspace.GrasslandsDestinations[tostring(i)]
humanoid:MoveTo(v.Position)
humanoid.MoveToFinished:Wait()
end
for i,_ in ipairs(workspace.GrasslandsDestination:GetChildren()) do
local v = workspace.GrasslandsDestinations[tostring(i)]
humanoid:MoveTo(v.Position)
humanoid.MoveToFinished:Wait()
end
This will be helpful if he decided to add more paths and he wonât need to touch the script to do that.
Thatâs a good idea. However, using ipairs is not as performant as a numeric loop (micro-optimization, ok). But you are also indexing stuff twice in each iteration.
You could do it by just replacing 6 by #workspace.GrasslandsDestination:GetChildren().
I would sort the destinations just in case. Also, is the order of instances not chronologically defined by their lifetimes (based on when you created those destinations)?
local destinations = workspace.GrasslandsDestinations:GetChildren()
table.sort(destinations, function(a, b)
return tonumber(a.Name) < tonumber(b.Name)
end)
for _, destination in ipairs(destinations) do
moveTowards(destination)
end
This approach have the equal outcome of @mishajonesâ approach. I like his approach better, since it is easier than using âtable.sortâ.
So a few hours later and I tried every method shown here. They all produce the same results even with a few modifications. The order in which the points are walked to is misaligned. I feel as if I did not give enough information so sorry about that. Here is more:
A key information when debugging is that shown in the output. Open Studio and then go to View > Output. Important messages such as errors are shown there. If you looked at the above examples, we assumed you could modify the script so it works for your use case. The output wouldâve let you know what went wrong.
This code should work:
local alien = script.Parent
local humanoid = alien:WaitForChild("Humanoid")
for i = 1, #workspace.GrasslandsDestinations:GetChildren() do
local v = workspace.GrasslandsDestinations["Grasslands" .. i]
humanoid:MoveTo(v.Position)
humanoid.MoveToFinished:Wait()
end
Please donât copy and paste the script - make sure you understand it.
Micro-optimization is mostly pointless. In this case it may even be less performant since Roblox optimises ipairs into simpler internal bytecode instructions.
A numeric loop would be more performant than ipairs regardless of any under-the-hood optimizations. But as you say, in most cases (including OPâs) the difference would be negligible.
Also, someone else already explained how to use table.sort in this threadâŚ
Ok, I fixed it myself. I put a wait(2) before the code started. This is weird because I used WaitForChild and that did not work but I used wait(2) and that fixed it.