Table is returning empty when it has content

I have a table that contains tables containing more information but when I go to read the table, it says there is nothing inside of the table.

I print debugged everything and everything worked perfectly fine except for the fact that the table is empty.

Code:

local Amount = 20
local BaseValue = 10
local BasePrice = 100

local Inventory = {} --The master table

local InventoryInformation = {}

function InventoryInformation:GetEconomy()
	print(Inventory, #Inventory) --Prints to table, 0
	for i,v in pairs(Inventory) do --Does not run as Inventory contains 0 indexes
		print(i, v)
	end
	return Inventory --Returns an empty table
end

function InventoryInformation:BuildEconomy()
	local LastValue
	local LastPrice
	for Index = 1, Amount do
		local InventorySet = {}
		if Index == 1 then
			local Information = {}
			
			Information["Price"] = BasePrice
			LastPrice = Information["Price"]
			
			Information["Value"] = BaseValue
			LastValue = Information["Value"]
			
			table.insert(InventorySet, Index)
			table.insert(InventorySet, Information)
			
			table.insert(Inventory, InventorySet) --Puts InventorySet inside of the master table
		else	
			local Information = {}
			
			Information["Price"] = LastPrice * 2
			LastPrice = Information["Price"]

			Information["Value"] = LastValue * 2
			LastValue = Information["Value"]
			
			table.insert(InventorySet, Index)
			table.insert(InventorySet, Information)
			
			table.insert(Inventory, InventorySet)
		end
	end
end

return InventoryInformation

Thanks to anyone who responds!

2 Likes

Whenever you require the ModuleScript, are you calling the Inventory:BuildEconomy() function before Inventory:GetEconomy()? If you call the Get function before the Build one, the table remains empty.

Maybe show us the script that requires the Module?

2 Likes

Yes BuildEconomy is called before I run GetEconomy and I setup printing and everything ran perfectly fine.

Can you show the script requiring it?

Script that calls build ecomomy:

--Services
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--Folders
local ServerModules = ServerStorage.ServerModules
local ReplicatedModules = ReplicatedStorage.ReplicatedModules

--Modules
local ItemInformation = require(ServerModules.ItemInformation)
local InventoryInformation = require(ReplicatedModules.InventoryInformation)
local PlayerHandler = require(script.PlayerHandler)
local ThrowHandler = require(script.ThrowHandler)
local SellHandler = require(script.SellHandler)
local ShopHandler = require(script.ShopHandler)
local CannonHandler = require(script.CannonHandler)
local ShopHandler = require(script.ShopHandler)
local InventoryShopHandler = require(script.InventoryShopHandler)

--Build Economy
ItemInformation:BuildEconomy()
InventoryInformation:BuildEconomy()

--Initiations
PlayerHandler:Initiate()
ThrowHandler:Initiate()
SellHandler:Initiate()
ShopHandler:Initiate()
ShopHandler:Initiate()
CannonHandler:Initiate()
InventoryShopHandler:Initiate()

Script that calls GetEconomy:

function SetupShop()
	for Index, Value in pairs(ScrollingFrame:GetChildren()) do
		if not Value:IsA("UIGridLayout") then
			Value:Destroy()
		end
	end
	
	local EconomySet = InventoryInformation:GetEconomy()
	for Index, Value in pairs(EconomySet) do
		local PurchaseButton = script.PurchaseButton:Clone()
		
		PurchaseButton.Capacity.Text = Value[2]["Value"]
		PurchaseButton.Price.Text = Value[2]["Price"]
		PurchaseButton.Parent = ScrollingFrame
	end
end

function OnInventoryShop()
	if Gui.Enabled == false then
		SetupShop()
		Gui.Enabled = true
		Frame:TweenSize(OriginalSize, Enum.EasingDirection.Out, Enum.EasingStyle.Bounce, 1, true)
	end
end

InventoryShopRemote.OnClientEvent:Connect(OnInventoryShop)

BuildEconomy also runs before GetEconomy is ever called

You can not call the BuildEconomy function from a server script and then obtain that table from a LocalScript. I see that you use the OnClientEvent event on your second part, so I infer that is it LocalScript instead of a server script. That is your problem.

Lua goes from top to bottom. The code will execute the engine from top to bottom. IN the code provided, your data is added AFTER the print statement.