How exactly do I do this

Viewed 48

In lua -- Im making a game where you need to sell items. I have two tables. One for items, and one for item costs.

itemcosts = {
  "Chewed Gum" == 5,
  "Leaf" == 2,
  "Lint" == 5,
  "Moldy bread crumbs" == 10
}
items = {"Leaf"}

How would I like specify the price? Like I tried

price = itemcosts[items[1]]

but It didn't work. Any help?

1 Answers

Initialize your itemcosts table like this:

itemcosts = {
  ["Chewed Gum"] = 5,
  Leaf = 2,
  Lint = 5,
  ["Moldy bread crumbs"] = 10
}

When you want to use a table key that's a valid name in Lua, you don't need quotes or anything around it; just use it directly. When it's not a valid name, such as if it has spaces in it in your case, or even if it weren't a string at all, then you need square brackets around it. Also, remember that == is for comparison and = is for assignment.

Related