GetChildren() always returning differently ordered values every time

I am making a plot selection screen and I get all the plots in the workspace into a table. But every time I run it changes. For example index 1 is sometimes plot 2 or plot1 or plot 2929300.
I don’t know what’s wrong the only thing I could find on the devforums was to use table.sort but I can’t get it to work. Even if that is the solution I don’t understand why because in the API it says that it orders the values by default
Here is an example of the table printed out:
image

Here is the actual folder:
image

If you would like them sorted, you will indeed need to use table.sort() using a name sort function.

The reason for this is due to a more recent change to Sibling Instance Ordering Sent at Game Load

1 Like

You will need to use ipairs for that

for I,v in ipairs(things:GetChildren) do

end
1 Like

Just to add to this, a name sort like this should suffice

table.sort(plotTable, function(a,b)
	local name1 = tonumber(a.Name:match("%d+"))
	local name2 = tonumber(b.Name:match("%d+"))
	return name1 < name2
end)

Where plotTable is the name of the varaible that contains a table of all the children in the Plots folder

This is a simple ascending sort method. What this those is when it begins to compares 2 indexes, it gets the name of the part and gets only the numbers in it, hence why there is "%d+", and since it’s a string, it’ll need to be converted to a number before comparison.

This was my result using it

image

First print was before sorting and second was after sorting. Hopefully it should do what you require, if you need them in descending order, just change the < to a >

@MightyDantheman Yours can work as well if @OP has it set up so the prefix is always the same, but mine ensures it’ll always compare the numbers in the event that one of the names is different, Plot1 instead of Plot 1 or plot 1 instead of Plot 1, which will order it differently due to how Roblox’s default sorting method accounts for ASCII codes, and lowercase and uppercase letters have different codes, so it would require to lowercase the names before comparision

table.sort(plotTable, function(a,b)
	return a.Name:lower() < b.Name:lower()
end)

In the end it depends on how they have it set up

I think you can even just do

table.sort(plotTable, function(a,b)
	return a.Name < b.Name
end)

as the default sort is alphabetical order, which I believe accounts for number order as well.

Okay so first of all I have no idea why but using that didn’t work!, but I am trying to wrap my head around it.
So, are a and b just 2 indexes of the table? So do are you checking if the first random index is smaller than a second one?
My understanding so far is: When it starts it checks 2 of the indexes on the table that are set to a and b. It then converts that to a string?(I don’t get why) Then it checks if a is smaller than b and if so it returns true? Why do we use a.Name and b.Name? Why do we convert it to a number? What does the :match("%d+") do? What are a and b?

Sorry I have so many questions its just that I find it really hard to wrap my head around I’ve been browsing all the different devforum answers on this and I’m struggling as to why this isn’t working?
Thank you so much for your time!

a and b are names given to the 2 returns of a function connected to table.sort, they’re just the 2 positional indexes. So if you have a table with 3 elements in it, table.sort first gives the 1st and 2nd thing, and then the 2nd and 3rd thing to compare.

What the code is supposed to do is first get the names of a and b and uses the string.match function to find numbers in it, "%d+" is a string pattern that finds all the matches of numbers in the name of the instance. But we cannot stop there as it’s still a string, so we have to convert it to a number via tonumber. Then we need to give it a condition to check, so we compare the 2nd number with the 1st, and if it’s false, it moves the first element forward, if it returns true, it keeps the positions the same.

How is it not working if I may ask?

1 Like

it seem to be the children of the part that is messing the order

plots=workspace.Plots:GetChildren()

table.sort(plots, function(a, b)
	return a.Name < b.Name
end)
for i,v in pairs(plots)do
	print(v)
end

When comparing two strings which greater than or lesser than and any variations of those operators it will assume the string’s length. This would never work.

I’ve checked and this doesn’t work for ever thing. I can’t find anything explaining the how in :GetChildren() gets the table of instances but I believe it to not allow for a numerically sorted array.

Ah, I think I’ve finally understood it. Thank you so much and sorry for responding after this long. I’ve finally got it to work. I realized the script that I was using the same folder in a different script and I had forgotten to sort that one in the script (and I was printing out from that one). I guess I should be using the same table in both the scripts :sweat_smile:. Anyways thank you so much for taking time of your day to clear my doubts! :grin:

1 Like

Anytime! if you have anymore issues don’t be afraid to make another post!

And don’t forget to mark a post as the solution since I saw you removed the old solution post!