How do you reference another value in the same table from inside the table?

local Table = {
    One = 5,
    Two = One + 3
}

How would I reference One while defining Two? I tried doing
Two = self.One + 3
but it didn’t work.

Is this even possible? Because I need this to make what I want to make.

1 Like

You would have to either manually define the key outside of the table or make a method and set the key from there.

What do you mean by this?

That you are asking is not possible at all. You should define your variables outside of the table and then add them to the table.

Ok, I found a workaround by defining each value individually

Table = {}
Table.One = 5
Table.Two = Table.One + 3

This might be what Omega or you said

2 Likes

Yep that’s one way to achieve what I mean.

I’m happy you found the solution :slight_smile: .

Keep coding!

1 Like

By the way if I helped you, please mark my answer as a solution in order to keep the rules in Roblox devforum.

local myTable = {
   value1 = 10,
   value2 = function()
      local result = self.value1 + 5
      return result
   end
}

print(myTable.value2()) -- Output: 15
1 Like

Yeah via method but that’s again after the table is defined. The function is called after that…