Access nested Dictionary in ModuleScript

I am trying to create a standard location for game Inventory items to be able to reference them for generating a ShopUI and Player data. I created a ModuleScript with the aim of having nested disctionaries(?) within it to store information.

local Inventory = {}
local Pets = {
	["Pet1"] = {image = 12345678, cost = 10, desc =  "A pet"};
	["Pet2"] = {image = 23456789, cost = 20, desc =  "A pet"};
}
local Weapons = {
	["Gun1"] = {image = 987654321, cost = 10, desc =  "A gun"};
}
return Inventory

I thought I could then loop through the Pets to clone a form with the required details, however ambition exceeds my skills once again, as when I try to reference

local inventory = require(game.ReplicatedStorage:WaitForChild("Inventory"))
local pets = inventory.Pets
print("Pets: ", pets)

The variables pets always shows as nil. I have tried creating the dictionary like so:

local Pets = {
	Pet1= {image = 12345678, cost = 10, desc =  "A pet"};
}

Neither seems to work. I just want to be able to create a variable that stores the Pets table from the ModuleScript, which I can then access to generate a Cloned form with all the details on for a Shop UI. How do I achieve this? I figure I a misunderstanding how to access that data within the module.

It appears youve forgotten to put Pets and Weapons in Inventory

You could do this by simply doing:

Inventory.Pets = { ...

or

local Inventory = {
    Pets = { ...
}
2 Likes

This won’t work because you are not looping through the items in the table. Try using for i, v in pairs() do

Code:

This code may not look as nice as I wrote this on phone.

—- add loop below existing code and remove the print statement 
for i, pets in pairs(inventory.Pets) do
 print(“Pets: “..pets)
 —-below this you can access the table by referring to it as pets.image or something like that.
end

Thanks. That’s fixed the problem so I can reference them now. I wasn’t really sure how to reference the info directly from the modulescript.

Thanks for the reply. It was more the fact that the variable was showing as nil that was throwing me.