My for loop is broken

I am trying to loop through everything in a module so I get the game’s versions BUT, for some unknown reason in this script “i” is a name of the table and “v” is nothing.

for i, v in pairs(main.IFRF) do
			print(i, v)
			local textlabel:TextButton = script.Parent.Parent.Parent.Parent.VersionList:WaitForChild(tostring(tonumber(i)))
			textlabel.Text = v
			textlabel.Visible = true
		end

image

This is the module in case you need to see it

local versions = {
	["IFRF"] = {
		["Super Plasma Core 475"] = {
			["Game Version"] = 475,
			["Progress"] = "Unknown",
			["Discontinued"] = true,
			["More Info"] = "This belongs to one of the first versions of IFRF."
		},
		
		["Super Plasma Core 917"] = {
			["Game Version"] = 917,
			["Progress"] = "Unknown",
			["Discontinued"] = true,
			["More Info"] = "This is a first revamp attempt, but it was a failure."
		},
		
		["Super Power Core 0.1"] = {
			["Game Version"] = 0.1,
			["Progress"] = "Unknown",
			["Discontinued"] = true,
			["More Info"] = "This is a second revamp attempt which was a success"
		}
	}
}

return versions

Please help

Unlike an array where i is the index, when looping through a dictionary i is the key and v is the value of that key.

local array = {
	"Value",
	"Value2"
}
local dictionary = {
	["Key"] = "Value",
	["Key2"] = "Value2",
}

for index, value in pairs(array) do
	print(index, value);
end
for key, value in pairs(dictionary) do
	print(key, value);
end

Output:

1 Value
2 Value2
Key Value
Key2 Value2
1 Like

Well how would I get an index from a dictionary then? If that is even possible. Without renaming them to 1, 2, 3, 4?

You can’t since dictionaries are unordered. It looks like you’re trying to loop through the dictionary and get the game versions of all the values, which you can do with value["Game Version"]:

for key, value in pairs(main.IFRF) do
    print(value["Game Version"])
end
1 Like

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