GetChildren() not returning in order

I am programming a chess engine in studio, and the engine reads a specific FEN code that reads the squares on the board, and what pieces are on them (if any). All of my workspace objects are organized in alphabetical order, but I am having issues with GetChildren() reading them out of order, breaking the FEN code function.

Here is my workspace, and all of the objects that are placed in alphabetical order:
Capture1

Here is a simplified version of my code that still shows the error:

local board = game.Workspace.Board

local function parseRow(rank)
	for i, square in pairs(rank:GetChildren()) do
		print(square)
	end
end

local function FEN()
	for i, rank in pairs(board:GetChildren()) do
		parseRow(rank)
	end
end

FEN()

Here is my output, which shows the squares, printed out of order:

What have I tried? I have tried calling a function that sorts the children of Workspace.board in the parseRow function, but the error remains the same.

Here is a copy of the studio file with only the board and script, in-which the error remains:
ScriptingSupport.rbxl (26.7 KB)

1 Like

I believe it returns back in the order from where it was first parented from (Or the order in what you inserted in first), to fix this I think you can use create a table that gets all the parts, then use table.sort afterwards?

2 Likes

You can’t rely on GetChildren’s order.

You could access the parts by hard-coded names:

local board = workspace.Board

local chars = {"A", "B", "C", "D", "E", "F", "G", "H"}

local function parseRow(row, model)
	for i = 1, 8 do
		local square = model:FindFirstChild(chars[i] .. tostring(i))
		print(square)
	end
end

local function FEN()
	for i = 1, 8 do
		local model = board:FindFirstChild("Row" .. tostring(i))
		parseRow(i, model)
	end
end

FEN()
1 Like

Wow, I guess I just assumed it was alphabetical. Thats pretty dumb, but it worked