Array not retaining list order after items are removed

just for a little context, i was working on this path making algorithm thing and once the path was fully built it calls a function which removes any dead ends. but after this function does it’s work, the tiles aren’t in the same order anymore?

video for a visual explaination


i just added a simple function that iterates through all the placed tiles using :GetChildren() and changes their colour to white. it iterates through each tile sequentially when no tiles are removed.

is this an intended feature? i thought if it acted like any normal array all the elements in front of the removed ones would move into the gap, rather than moving the elements at the end into it. the :GetChildren() documentation also says that the array should be in that order,
image

is there any way around this other than making something like a linked list with the tiles as elements if i want them to stay in order?

Do you have any code that you could send?

:GetChildren() is not guaranteed to be in any specific order. that documentation page definitely needs to be updated, it’s a simplification of what’s going on behind the scenes. that behavior is just an artifact of how the current code consolidates children, but you shouldn’t rely on it. there are also edge cases where that might not be exactly right, like what you’re running into right now. the easiest way to avoid this is just to cache the array yourself so that you have a definite order.

2 Likes

Following up on this.. as it is most likely the issue here.
To preserve order, use GetChildren() → sort manually by Name or … LayoutOrder (for GUIs).

local c = folder:GetChildren()
table.sort(c, function(a, b) return a.Name < b.Name end)
1 Like

this works. thanks for the help!

1 Like

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