Multiple Variables in a Table? (Help me)

Okay so basically what I am trying to do is add a catalog for a clothing store, however I have an idea of adding a table to make it easy to add clothing and what not.

I have the basic Idea that you can do tables such as the following
local Table = { “shirt1”, “shirt2”, “shirt3” }

however I want to add the shirt name and its ID so something like
local Table = {
“shirt1”, ID = 234324,
“shirt2”, ID = 324325
"shirt3", ID = 654644
}

So then when I go to
print(MyTable[1]) it will print the Shirt and ID.

I know this obviously wont work, but how would I make this work? I have an idea of another way but I am just wondering if there is a simpler way of doing things. Thanks!

1 Like

You could just do this:

local shirts = {
   shirt1 = 234324,
   shirt2 = 324325,
   shirt3 = 654644
}

print(shirts.shirt1)
2 Likes

You could do essentially multiple dimensional array’s however I would recommend doing an array like this so it is a dictionary

local Table = {
[“Vine’sShirt”] = 143214;
[“Marco’sShirt”] = 321413;
}

You can loop through it doing
for Name,Id in pairs (Table) do
print(Name)
print(Id)
end

If you want to go for multi tables you could do

Table = {

}

Table[“Shirt”] = {[“Id”] = 13141; [“ShirtName”] = “Meh”}

print(Table[“Shirt”][“Id”])

3 Likes

Well basically Shirt 1 is going to be the name of the shirt and the ID will follow after, so I need them in separate Variables so I can print both the shirt name and the shirt ID

These are called dictionaries if you want to learn more
http://wiki.roblox.com/index.php?title=Table&redirect=no#Dictionaries

1 Like

If you want to give it more info, you can split it up into sub-tables too:

local shirts = {
   shirt1 = {ID = 123456},
   shirt2 = {ID = 654321}
}

print(shirts.shirt1.ID)
7 Likes

Alright this is the best one, and how would I print all of them for example:

local shirts = {
shirt1 = {ID = 123456, Name = “Red Shirt”},
shirt2 = {ID = 654321, Name = “Red Shirt”}
}

for i=1, #shirts do
print("Shirt ID: "…shirts[i].ID)
print("Shirt Name: "…shirts[i].Name)
end

How would I print all of the shirts and the ID’s in the table?

local shirts = {
	shirt1 = {ID = 123456, Name = “Red Shirt”},
	shirt2 = {ID = 654321, Name = “Red Shirt”}
}

for shirtKey,shirt in pairs(shirts) do
	print(shirt.ID, shirt.Name)
end
1 Like

Never mind I got it
Thank you all!

"local shirts = {
{ID = 123456, Name = “Red Shirt”},
{ID = 654321, Name = “Red Shirt”}
}

for i=1, #shirts do
print("Shirt ID: "…shirts[i].ID)
print("Shirt Name: “…shirts[i].Name)
end

3 Likes

Also, FYI you can surround your code with ``` to format it! Not three single-quotes, but three backticks (the weird symbol usually under your escape key)

2 Likes

You’ve already worked out a solution, but I want to mention that @Crazyman32’s original solution can be used for what you want, and in my opinion is better because it doesn’t require iteration or knowing the exact integer key number to get data about a specific shirt, it’s as easy as printing shirts[“Red Shirt”].

If you need to loop over that dictionary, you can actually print the key as well as the value using a generic for. Here’s an example:

local shirts = {
["Red Shirt"] = 12341234,
["Blue Shirt"] = 4171524,
["Yellow Shirt"] = 18356224,
}

for key,val in next,shirts do
print("Shirt name is "..key..", shirt ID is "..val)
-- >> Shirt name is Red Shirt, shirt ID is 12341234
-- etc etc for all the shirts
end

-- and if you want to get the value about a specific shirt name, say
print(shirts["Yellow Shirt"])
-- >> 18356224
5 Likes