Whats the best way to get objects by progression in a in pairs loop
like
Part 1
Part 2
Part 3
and avoid like
Part 2
Part 1
Part 3
Use ipairs
instead of pairs
.
Hopefully this works I just got to research how to use it I will confirm if this is a solution after I’m doing learning how to use ipairs
You just need to replace pairs
with ipairs
. Both are similar except ipairs
iterates over numerical indices such as 1, 2, 3, etc.
ipairs
uses an iteration that is in order, rather than pairs
unordered method. It can’t be more simple than that. StackOverflow provided some answers on that:
I’m not positive but I don’t think ipairs will help what I’m trying to do Unless I’m not understanding how ipairs work correctly
Can you post your code so we can better help you?
I don’t have a code yet but just want to know how would I get objects in a correct order I want it to be for like example
part1,part2,part3 are parts and are located in a folder in the workspace but yunno how i,v in pairs I believe it gets objects in an alphabetic order so who knows how in pairs will react and put them in a wrong order of like {part2,part3,part1} so how would I force it to get everything in the order I want using numbers ask any questions if im not clear enough and you are confused what I’m asking for
@COUNTYL1MITS has the right idea here. ipairs
will iterate through a table in order until there is nothing else to run through, or until the value in an index is nil
. However, the objects must already be in order in the folder if you want this to work as expected.
Code Sample:
for index, obj in ipairs(Folder:GetChildren()) do
print(obj.Name)
end
--After running, this should print
--[[
Part1
Part2
Part3
]]
Another way you could go about this problem is to use a numeric for loop
and concatenate the original part name with the number that is currently going through.
local parts = workspace.Folder:GetChildren() --Change as neccessary
for currentNumber = 1, #parts do
local part = parts["Part".. currentNumber]
print(part.Name) --The name is the exact same as the concatenation on the line above.
end
Using ipairs
shouldn’t solve the issue. This is because it will iterate through the table in order of the keys that GetChildren
returns. According to the DevHub,
The children are sorted by the order in which their
Parent
property was set to the object.
So to the sort table we can call table.sort
with our table and a sorting function that uses each instances name.
local children = theParent:GetChildren()
table.sort(children, function(part1, part2)
return part1.Name < part2.Name
end)
for _, child in pairs(children) do -- In this scenario ipairs is optional
-- code
end
Please do not post false information.
pairs()
iterates in the table’s natural order of key-value pairs. I won’t get into the details, but this order is unpredictable because Lua’s hash function rearranges its hashtables every new index.
ipairs()
iterates in order of numerical indexes, not in order of values
For example, let’s say I have I define an empty table and add the following key-value pairs:
local myTable = {}
myTable[3] = "c"
myTable[1] = "a"
myTable[2] = "b"
comparing the outputs of pairs()
and ipairs()
would look like this
for _, value in pairs(myTable) do
print(value)
end
-- b a c (random)
for _, value in pairs(myTable) do
print(value)
end
-- a b c (in order of the indexes: 1, 2, 3)
The for currentNumber = 1, #table do end worked
but the ipairs did not my code was
for i,v in ipairs(game.Workspace.Folder:GetChildren()) do
print(v)
end
this is how the objects looked like in workspace
this is what the output gave me
but as you said the objects have to be already in order
:GetChildren() returns an array of an object’s children. What I meant to say was that this array should be sorted so that it is in order. However, if sorting is required, you can use a little less memory by simply using the numeric for loop
.