Nested Tables need specific table, not all

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want to call a table and iterated over ONLY that table. I can call the nested table and make a copy easy from the documentation on the table document dev info. I copied that easy done… However, I do not understand how to copy a table inside a table AND ONLY that table… I have tried if nestedtable == “Inventory” for example, it still calls the ENTIRE table.

  1. What is the issue? Include screenshots / videos if possible!
    Read above, decribed issue.

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried to look it up but all documentation and people just want to print the table, no one has talked about selecting that table only and using that information to work with.


Tables:
local module = {
         gold = 1,
         exp = 0,
	 Inventory = {
			Wood = 1,
			Stone = 0,
	 }
}
	local function copyInventory(data)
		local inventoryCopy = {}
		for tableData, nestedTable in pairs(data) do
			if nestedTable == "Inventory" then
				nestedTable = copyInventory(nestedTable)
			end
			inventoryCopy[tableData] = nestedTable
		end
		return inventoryCopy
	end
	
	local newInvCopy = copyInventory(data)
	for itemName, itemCount in pairs(newInvCopy) do
		local button = Template:Clone()
		button.Name = tostring(itemName)
		button.Visible = true
		button.Parent = List
		button.Text = tostring(itemName)
		button.Count.Text = tostring(itemCount)
	end	

This script makes a button in a list and it always, does the whole table, I have tried writing it many different ways but maybe I am just thinking too hard on something. I don’t want the GUI to have the information from the entire module, only the inventory. However since I am using PS to save data, I haven’t really figured out how to make seperate tables I have always just had a template and when a player joins I do the PS thing and copy that template. Everything works fine with saving data, but I cannot seem to get just the inventory table ONLY.

1 Like

Wow, I watched one youtube video and realized how more complecated I made this. Nested tables can be called just like normal tables, honestly I have no idea why people bother iterating through them so much. I fixed my code by just referencing the table directly I thought that you couldn’t do that with nested tables… I thought you had to clone them or something.

Fixed version:

	local Inventory = data["Inventory"]
	for itemName, itemCount in pairs(Inventory) do
		local button = Template:Clone()
		button.Name = itemName
		button.Visible = true
		button.Parent = List
		button.Text = itemName
		button.Count.Text = itemCount
	end
1 Like

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