Need help with Tables (finding a table inside another table?)

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

  1. What do you want to achieve? A friend asked me to script a “wardrobe system”, similar to Royale High’s one. For those who are not familiar with it, here’s how it looks like: image
    Basically, it let you sort each piece of clothing by Color and Style. Each outfit can be worn in-game (for free) or purchased using MarketplaceService.

  2. What is the issue? While I managed to make the sorting by color work using Value objects, I can’t figure out how to sort by Style. What I’d like to understand, is how I can set the “Style” value to the table’s name where each ID is. Here is the code:

local MS = game:GetService("MarketplaceService")
local Player = game.Players.LocalPlayer
local ClothesTable = require(script:WaitForChild("Clothes"))

local ClothesFrame = script.Parent.Main:WaitForChild("Clothes")
local ClothesTemplate = script:WaitForChild("Sample")

function getClothes()
	for style,pieces in pairs (ClothesTable) do
		for id,color in pairs(pieces) do
--The template part contains some Values containing the type of asset (shirt or pants), color and style. I can't figure out how to set the Style value to the table's name.
			local CloneTemplate = ClothesTemplate:Clone()
			local Purchase = CloneTemplate.Buy
			local PI = MS:GetProductInfo(id)
			
			CloneTemplate.Name = id
			CloneTemplate.Type.Value = PI.AssetTypeId
			CloneTemplate.Color.Value = color
			CloneTemplate.Parent = ClothesFrame
			CloneTemplate.Image = "https://www.roblox.com/asset-thumbnail/image?assetId="..id.."&width=420&height=420&format=png"

			Purchase.Activated:Connect(function()
				MS:PromptPurchase(Player, id)
			end)
		end

	end
	ClothesTemplate:Destroy()
end
getClothes()


local SelectionsFrame = script.Parent.Main:WaitForChild("Selections")
local ColorButtons = SelectionsFrame.Colors:GetDescendants()
local StyleButtons = SelectionsFrame.Styles:GetDescendants()


--Sort by Color
for i,button in pairs(ColorButtons) do
	if button:IsA("TextButton") then
		button.Activated:Connect(function()
			for i,v in pairs(ClothesFrame:GetChildren()) do
				if v:IsA("ImageButton") then
					if button.Name:lower() == v.Color.Value then
						v.Visible = true
					else
						v.Visible = false
					end
				end
			end
		end)
	end
end

And here is the ModuleScript’s table with all the clothes IDs (it’s a bunch of random stuff i found in the catalog):

local Clothes = {
    ["Glam"] = {
        [6524540459] = "blue",
        [6477704287] = "red",
        [6444593041] = "gold",
        [5631240312] = "silver"
    };

    ["Dark"] = {
        [6500360755] = "black",
        [6379790284] = "white",
        
    };

    ["Sporty"] = {
        [6544455379] = "red",
        [6518310486] = "black",
    };
    
    ["Fantasy"] = {
        [2033670602] = "gold",
        [6466806792] = "pink"
    };
    
    ["Casual"] = {
        [5091477772] = "blue",
        [5494529238] = "yellow",
        [6268143988] = "red",
        [6267187487] = "green",
        [6532665725] = "purple",
        [6530714781] = "other"
    };
    
    ["Men"] = {
        [6539097465] = "other",
    };
    
}
  1. What solutions have you tried so far? I tried using table.find and table.unpack, but the last one simply returns nil. I’ve never worked with Tables like this before, so I’m quite lost. I probably made some mistakes in the way I set up everything.

Some parts of this post might be confusing, I apologize for that, so feel free to ask if you are unsure. I’ll try to explain the best way I can. Thanks in advance. :slight_smile:

1 Like

Hey!

I looked at your code, and if I understood the issue correctly, you are trying to let the code find out what the styles name is, which contains the ids and colors.

The “style” variable in

for style,pieces in pairs (ClothesTable) do

defines it.

So, to get the value of it, all you need is this variable.

print(style)
–For example, this prints “Glam”

I hope I could help, if you need any other help, I’d like to try and help you out!

you can do like uhh
clothes[“Glam”][6524540459]
or do
clothes[“Glam”][“blue”]
by switching it around idk

uwu

2 Likes

Hey, thank you so much! You were right, I completely forgot about that style variable in the for loop lol.
The script now does assign the correct string to the style StringValue but when I try to sort the clothes in the GUI, nothing shows up. I used the same code in the first post (under the “Sort by Color” comment) but unfortunately instead of setting Visible = false only on the clothes with a different Style value than the one selected, everything is made invisible. I changed the code like this:

for i,button in pairs(StyleButtons) do
    if button:IsA("TextButton") then
        button.Activated:Connect(function()
            for i,v in pairs(ClothesFrame:GetChildren()) do
                if v:IsA("ImageButton") then
                    if button.Name:lower() == v:FindFirstChild("Style").Value then
                        v.Visible = true
                    else
                        v.Visible = false
                    end
                end
            end
        end)
    end
end

Any clue on what I’m doing wrong?

I think the problem is that only the buttons name is lowered.

Try adding a :lower() to the the Value, like this:

v:FindFirstChild(“Style”).Value:lower()

1 Like

Omg yes, I figured it out literally 30 minutes after posting my last message LOL. I feel so dumb, if I didn’t spend 2h without breaks working on this I would’ve probably figured out everything by myself. Thank you so much for helping! :slight_smile: