Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

What are the purpose of tables?

Asked by 9 years ago

Does anyone have any examples on why tables are important in Lua scripting?

1
Tables are important because they can store multiple values, which leads to many possibilities, such as randomizing. RedCombee 585 — 9y

2 answers

Log in to vote
3
Answered by 9 years ago

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}

http://wiki.roblox.com/index.php?title=Tables

1
Thanks. soulman678 25 — 9y
Ad
Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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

Answer this question