Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do i generate a dictionary?

Asked by 5 years ago

How do i make a dictionary with the table.insert function? Like when i have a table products and i want to fill it with the amount and price of each product through a script.

0
You can better do TABLE[ITEMORITEMNAMEASASTRING] = VALUE User#20388 0 — 5y

1 answer

Log in to vote
0
Answered by
mattscy 3725 Moderation Voter Community Moderator
5 years ago

If you have a bunch of product names, and values for each, you can set up a dictionary like this to hold that info:

local Products = {
    Banana = {
        Amount = 5;
        Price = 4.5;
    };
    ["Orange Peel"] = {
        Amount = 666;
        Price = 6.7;
    };
    --etc
}

An then, if you wanted to add a new product with a script, you could do something like this:

Products[Apple] = {
    Amount = 3;
    Price = 2.3;
}

To then retrieve information about a product, you can index the dictionary like this:

local BananaPrice = Products.Banana.Price
print(BananaPrice) --> 4.5

Or, if there are spaces in the name of the product, you could do it like this instead:

local PeelAmount = Products["Orange Peel"].Amount
print(PeelAmount) --> 666

Hope this helps!

0
that helped thx Jabba171 16 — 5y
Ad

Answer this question