Does anyone have any examples on why tables are important in Lua scripting?
A table is a data type in Lua that is useful to store multiple values, including numbers, strings, functions, more tables, and much more. It is called a table because it acts like a grid with two columns
Example:
local myArray = {"A string", 3.14159, Workspace.Part}
Lua tables store "values" at different "keys".
They are used primarily as lists, objects, and tuples.
Since they are implemented as hashtables, they can also be used as dictionaries.
Since tables let you use other values as keys, you no longer need to give a unique name to each value you are referring to when you use a table, e.g.,
local pets = {"dog","cat","parrot"}; local myPet = 2; print( pets[myPet] ); -- cat
vs
local pet1 = "dog"; local pet2 = "cat" local pet3 = "parrot"; local myPet = 2; if myPet == 1 then print(pet1); elseif myPet == 2 then print(pet2); elseif myPet == 3 then print(pet3); end
They can also be used to store several named values in one variable.
local person = {}; person.name = "Bob"; person.pet = "cat"; print(person.name .. " owns a " .. person.cat); -- Bob owns a cat