Help using loops

Hello guys, this is a stupid question by needed
I have been trying to do an app center and for it I am using loops. To be more specific for
Here I will give you the sample code, my question is, how do you make it so the questions appear in the order they are in the child.

I want it to appear like 1, 2, 3, 4, 5, 6
But appears in total random numbers like 3, 1, 4, 5, 6, 2.

Event.OnServerEvent:Connect(function(plr, gui, Type) 
	local BackgroundQuestion = Color3.new(204, 204, 204)
	local QuestionColor = Color3.new(76, 186, 229)
	
	for Type, questions in pairs(script[Type]:GetChildren()) do
				local P = script.Question:Clone()
				P.Parent = gui.Frame
				P.Text = questions.Value
	end		
end)

The clonning works perfectly and stuff, do not worry for that.

it’s not quite obvious, but ipairs also exists, unlike pairs, it will not change the order at all.

More information : What is the difference between ipairs and pairs in for loops?

I tried ipairs but it gives me an error with a red underline, saying it expected in pairs. But will check thank you.

It stills shows in disorder Screenshot by Lightshot

Roblox sorts Instances based on when they were parented, and not alphabetically. This is why you get annoying overlay bugs when designing UI and not using the ZIndex property if you accidentally move a frame. Ctrl+Z can only fix so much.

Instead, use a numerical for loop:

for i = 1, #whereever:GetChildren() do
    print(whereever:FindFirstChild(i))
end
2 Likes

I will try this then I guess. I put then #script[Type]:GetChildren() right?

Alternatively, you can use table.sort with the function returning a inequality between the Instance names.
For example:

local Children = YourInstance:GetChildren()
table.sort(Children, function(a, b)
    return string.lower(a.Name) < string.lower(b.Name) --// Order by name value
end)

for Type, Questions in ipairs(Children) do --// Orderly iterate using ipairs
    --// Code
end

This’ll work for anything, and will put it in alphebetical order.

2 Likes

Alternatively, alternatively! You could iterate the folder and then input them in a table and you will get them in the same order as shown in your screenshot.

Like this:


local list = {}
for key, value in pairs (Values) do
	list[#list+1] = {key = key, value = value}
end
--and then
for type, questions in ipairs(list) do
--code
end	

This is nice but I think it is less complex using Returned’s solution.