Tables and Values not matching?

Basically, I want “Def(number)” to be matched with the table number. For example, if I have Def3, I want it to be 3rd in the table. However, the image shown below does not do that.

Note: I have done this before (Weeks 13, 14), but this Week (week 15) doesn’t work. You can see the Weeks in Explorer in the image below.

image

Why is Def3 and Def11 at the bottom? I have no idea why.

2 Likes

Because the way :getChildren works isnt by some sorting of the names, the way :getChildren is a bit more complicated and it probably won’t help you much even if I explained it. In any way, if you need to sort them into the table so their indexed match with their number, you can make a loop function to sort them for you.

To make it as simple as possible, I would recommend you to rename all the “Def1” into just “1”, so you dont have to filter the strings and just convert the string name into the number. After you renamed them all in roblox, create an empty table for the sorted values, then make a pairs loop to iterate through the children and assign every value to an index of its name converted with tonumber() function

3 Likes

Of course, you can do it slightly differently, and you’ll have to adjust the number of values, but you can do it like this

local Table = {}
local Folder = script.Parent
local MaxValue = #Folder:GetChildren()
print(MaxValue)
for X = 1,MaxValue do
	table.insert(Table,Folder:FindFirstChild("Def"..X))
end
3 Likes

This should work:

local folder = script.Parent.Define
local folder_table = folder:GetChildren()
local sorted_table = {}

for i, v in ipairs(folder_table) do
	table.insert(sorted_table, v)
end

print(sorted_table)
3 Likes

I have done this before with other values, using something similar.

Example:
image

However, this one doesn’t seem to work for some reason. Maybe something with the naming?

2 Likes

Currently, it is probably by the order you created them as instances, however I myself am not absolutely sure in this. In any way, you can’t rely on them being matching on a running server from this procedure, if you want the indexes and values to match with their names, you will have to make a sorting function of some kind.

1 Like

As others have said, GetChildren’s order isn’t defined and shouldn’t be relied on. It can at times, seem like it’ll be one way, but a change to the engine could break that behavior the next day. You should definitely sort that table if you want a defined ordering

1 Like

Thanks y’all, I’ll learn from this. :slight_smile:

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