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:
local IN = Instance.new("Part");
IN["BrickColor"] = BrickColor.new("Toothpaste");
IN.Parent = workspace;
Or, you can use keys in a way to index an Instance's children:
local IN = Instance.new("Part");
local NewIN = Instance.new("Part", IN);
NewIN.Name = "New Instance";
IN.Parent = workspace;
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:
local Table = {};
local value = "Hello, World!"
Table['key'] = value;
local MyValue = Table['key'];
Generating a table using hardcoded indices, then getting the value.
local Table = {
one = true;
two = false;
three = "three";
}
local OneValue, TwoValue, ThreeValue = Table['one'], Table['two'], Table['three'];
print(OneValue, TwoValue, ThreeValue)
Using keys within a table itself:
local Table = {
["Thwack"] = {
Damage = 45;
Description = "THWACK (nintendo please nerf hero i beg of you)";
};
["Whack"] = {
Damage = 25;
Description = "hero doesn't deserve this power"
}
["Inside of Party"] = true;
};
print(Table.Thwack, Table['Thwack']);
print(Table.Whack, Table['Whack']);
pcall(function()
print(Table.Inside Of Party)
end);
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
1 comment
Thanks, it worked! I've never knew you could use keys with tables.