Ipairs loop of a folder starts at child #2

Hey there!
So, I have a script where certain frames are parented once an event is fired. However, I want to do it in numerical order for an inventory, starting at 1 (obviously)
This is the code I used:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remotes = ReplicatedStorage.Remotes

remotes.ItemUpdate.OnClientEvent:Connect(function()
	for i, v in ipairs(script.InventorySlots:GetChildren()) do
		print(v)
		v.Parent = script.Parent.Frame
		break
	end
end)

Now, here’s the thing. It starts at 2, but works fine past that.


image
On the explorer, 1 does come first. (The text isnt the problem either, frame 1 has the text of 1, etc.)
image

I also tried using FindFirstChild, but it had the same outcome
How can I fix this (or is there an alternative to using a for loop)?

UIGridLayoutOrder >> List Layout
For each frame v.LayoutOrder = tonumber(v.Name)
Reading your problem and images isn’t enough to properly understand what you actual problem is as you’ve only stated you want it in numerical order yet inventory slots vs hotbar capacity are different

LayoutOrder starts from “0” so n-1 would suffice for each frame.

It doesn’t matter where the lowest number starts?? I’ve used LayoutOrder numerous times and I’ve never once had to “start from 0”

Hence suffice, I didn’t state that it was required.

Tried that, didn’t work
image

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remotes = ReplicatedStorage.Remotes

for i, v in pairs(script.InventorySlots:GetChildren()) do
	v.LayoutOrder = tonumber(v.Name)
end

remotes.ItemUpdate.OnClientEvent:Connect(function()
	for i, v in ipairs(script.InventorySlots:GetChildren()) do
		print(v)
		v.Parent = script.Parent.Frame
		break
	end
end)

The problem is that its choosing frame number 2, not that it appears in the wrong order when parented to the frame with the UIGridLayout.

I don’t understand why this is an issue for you though. Regardless if you want it to Re-parent in order then you will need to first create a table with the children, then use table.sort.
Example:

local myFrames = script.InventorySlots:GetChildren()
table.sort(myFrames,function(a,b)
    return tonumber(a.Name) < tonumber(b.Name)
end)