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!
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
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
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)
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