Answered by
5 years ago Edited 5 years ago
You use keys. Keys can be almost anything--userdata, strings, numbers, and even booleans. However, you probably shouldn't rely on userdata for a key.
Keys aren't specifically for tables however, they're like fancy dot notation (dot notation being Part.Mesh.Scale).
You can use keys in such a way to index properties of an Instance:
1 | local IN = Instance.new( "Part" ); |
2 | IN [ "BrickColor" ] = BrickColor.new( "Toothpaste" ); |
Or, you can use keys in a way to index an Instance's children:
1 | local IN = Instance.new( "Part" ); |
2 | local NewIN = Instance.new( "Part" , IN); |
3 | NewIN.Name = "New Instance" ; |
6 | print (IN [ 'New Instance' ] .Size); |
To index a table using a key, you use the square brackets (note, SQUARE brackets, not parenthesis.)
For example:
Using a Key to make indices within a table:
2 | local value = "Hello, World!" |
6 | local MyValue = Table [ 'key' ] ; |
Generating a table using hardcoded indices, then getting the value.
7 | local OneValue, TwoValue, ThreeValue = Table [ 'one' ] , Table [ 'two' ] , Table [ 'three' ] ; |
8 | print (OneValue, TwoValue, ThreeValue) |
Using keys within a table itself:
04 | Description = "THWACK (nintendo please nerf hero i beg of you)" ; |
08 | Description = "hero doesn't deserve this power" |
11 | [ "Inside of Party" ] = true ; |
15 | print (Table.Thwack, Table [ 'Thwack' ] ); |
16 | print (Table.Whack, Table [ 'Whack' ] ); |
18 | print (Table.Inside Of Party) |
20 | print (Table [ "Inside of Party" ] ); |
Resources
Be sure to look up resources to further develop your knowledge about tables. Tables are a major contender for one of the most useful basic abilities for a language.
Lua-Users: http://lua-users.org/wiki/TablesTutorial