How to access dictionary in the same dictionary

I have module with dictionary containing all the items and its properties, like this:

local ItemsInfoModule = {
	["Stone"] = {
		Name = "Stone";
		Type = "Resource";
		Slot = nil,
		StackCap = 3;
		Value = 1;
		Description = "Stone, value: " .. tostring(ItemsInfoModule.Stone.Value);
	};
}

return ItemsInfoModule

Description returns nil. How can i put items Value property inside Description property? I tried putting function inside this module but it just doesnt find it when i use it in Description:

function GetPropertyOfItem(item, property)
    return ItemsInfoModule[item][property]
end

--the Description inside Stone item:
Description = GetPropertyOfItem("Stone", "Value");
--calls a nil value
1 Like

How is ItemsInfoModule indexed (if both of those pieces of codes are from two different scripts)?

I think i was unclear. Both code snippets are two attempts to solve the problem but they are both only one module script. In the second example i just shorted the code so as not to repeat myself. Whole second example:

local ItemsInfoModule = {
	["Stone"] = {
		Name = "Stone";
		Type = "Resource";
		Slot = nil,
		StackCap = 3;
		Value = 1;
		Description = GetPropertyOfItem("Stone", "Value");
	};
}

function GetPropertyOfItem(item, property)
	return ItemsInfoModule[item][property]
end

return ItemsInfoModule

And then i access it from different script:

local ItemsInfoModule = require(ServerScriptService.ItemsInfoModule)
print(ItemsInfoModule["Stone"].Description)

Try using something similar to OOP:

local ItemsInfoModule = {
	["Stone"] = {
		Name = "Stone";
		Type = "Resource";
		Slot = nil,
		StackCap = 3;
		Value = 1;
		Description = GetPropertyOfItem("Stone", "Value");
	};
}

function ItemsInfoModule:GetPropertyOfItem(item, property)
	return self[item][property]
end

return ItemsInfoModule

It wont work because GetPropertyOfItem in line 8 is not defined

Sorry, what do you mean it’s not defined?

Its highlighted "Unknown Global “GetPropertyOfItem” " and when executed it attempts to call nil

Could you please show me your full code? Both sides (module and normal script)

–Module script:

local ItemsInfoModule = {
	["Stone"] = {
		Name = "Stone";
		Type = "Resource";
		Slot = nil,
		StackCap = 3;
		Value = 1;
		Description = GetPropertyOfItem("Stone", "Value");
	};
}

function ItemsInfoModule:GetPropertyOfItem(item, property)
	return self[item][property]
end

return ItemsInfoModule

–LocalScript:

local ItemsInfoModule = require(game:GetService("ReplicatedStorage"):WaitForChild("ItemsInfoModule"))

print(ItemsInfoModule["Stone"].Description)

Try putting the function before the items info

i think the reason it does not work because on line 8 Description = GetPropertyOfItem("Stone", "Value");, it tries to call GetPropertyOfItem, when it is not yet created. Making a function local or not does not make a difference if you call it before it was made

You can’t, at least not directly. The table is not actually created until the closing bracket.

Define it afterwards:


local ItemsInfoModule = {
	["Stone"] = {
		Name = "Stone";
		Type = "Resource";
		Slot = nil,
		StackCap = 3;
		Value = 1;
	};
}

ItemsInfoModule.Stone.Description = "Stone, value: " .. tostring(ItemsInfoModule.Stone.Value)

return ItemsInfoModule

Or fix your design so you’re not storing data in two places. For example, just store the description in the description and concat the value when it’s time to show the user.

This helped me a ton actually. I didn’t know I could store dictionaries inside a module script!