How to loop through a dictionary

So basically I want to loop through a dictionary in order to retrieve data about a hatched pet.

local module = {
	["Pets"] = {
		["Cat"] = {
			["Name"] = "Cat";
			["Rarity"] = "Common";
			["CoinMultiplier"] = 0.3;
			["GemMultiplier"] = 0.2;
			["RarityNumber"] = 43;
		},

		["Dog"] = {
			["Name"] = "Dog";
			["Rarity"] = "Uncommon";
			["CoinMultiplier"] = 0.35;
			["GemMultiplier"] = 0.25;
			["RarityNumber"] = 42;
		},

		["Dragon"] = {
			["Name"] = "Dragon";
			["Rarity"] = "Rare";
			["CoinMultiplier"] = 0.38;
			["GemMultiplier"] = 0.28;
			["RarityNumber"] = 15;
		}
	}
}

return module

You can just do a for loop with ipairs…

Doesn’t seem to be working.

local mod = require(game.ReplicatedStorage:WaitForChild("Assets").Modules.GlobalPets)

for i, v in ipairs(mod) do
	print(v)
end

ipairs does not loop through dictionaries. It loops through arrays. Use pairs instead.

6 Likes

or you can just do

for i,v in mod do 
    print(v)
end
2 Likes

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