Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Iterator not iterating in order?

So I have this dictionary:

local roundCycle = {
	["Game Intermission"] = {1, 30},
	["Round 1"] = {3, 0},
	["Round 1 Intermission"] = {1, 30},
	["Round 2"] = {3, 0},
	["Round 2 Intermission"] = {1, 30},
	["Round 3"] = {3, 0},
	["Round 3 Intermission"] = {1, 30},
	["Round 4"] = {3, 0},
	["End Game"] = {1,0}
}

Ideally, this dictionary should print from top-down order, but when I test it out, the iterator appears to process the dictionary terms in no apparent order. The code below is what I use to test the dictionary out:

for i, v in pairs(roundCycle) do
	print(i)
end

Is there any way I could fix this so it can print out in first to last order?

1 answer

If you don't make this a dictionary, then the hashmap of positive numerical keys can be used to easily reference values you need from the table.

Move(what were) the string keys of the dictionary into the first index of an array.

local roundCycle = {
	{"Game Intermission",1,30};
	{"Round 1",3,0};
	{"Round 1 Intermission",1, 30};
	{"Round 2",3, 0};
	{"Round 2 Intermission",1, 30};
	{"Round 3",3, 0},
	{"Round 3 Intermission",1, 30},
	{"Round 4",3, 0},
	{"End Game",1,0}
}

--Now, use a numerical for loop with the #operator to iterate through the table.
for i = 1,#roundCycle do
	local set = roundCycle[i];
	print(set[1]); --index other vals like: "set[2]" and "set[3]"
end

This discussion is preserved as it was. New answers, comments, and votes are closed.