Table indexing help

So I want to do something like this

local mission = {
Dummy = {"Amount" == 3, "Money" == 300, "Exp" == 300}  
}

How would I index the amount which is 3

Thanks

2 Likes

You’d have to put [ ] around “Amount”, “Money”, and “Exp”, and change your == to = but:

local amount = mission.Dummy.Amount

print(amount) -- 3

in total:

local mission = {
    Dummy = {
        ["Amount"] = 3; ["Money"] = 300; ["Exp"] = 300;
       Amount = 3; Money = 300; Exp = 300; -- this would also work.
    }
}

4 Likes
local mission = {
Dummy = {"Amount" = 3, "Money" = 300, "Exp" = 300}  
}

“==” is only for statements like if statements
“=” is for assigning variables

1 Like

As it turns out, this is valid syntax. Since the code uses == instead of =, the three expressions inside of the array evaluate to booleans and it is the same as:

local mission = {
    Dummy = {false, false, false}
}

Which makes Dummy a table of three boolean values at indexes 1, 2, and 3. This may be why you were having issues and no errors were showing up. What a tricky problem!

3 Likes

Thank you

And to everyone else who replied

You could also do syntax like

Table = {ObjectOne = "Five", ObjectTwo = "Four"}

It’s the same as doing ["ObjectOne"] or "ObjectOne"