Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Tables

When would be the right time to use tables? How would you apply it and also, how does it vairy from Variables.

2 answers

✓ Accepted answer

A table has lots of uses, and with lots I mean a real lot. A cool technical note is that the only data type in Lua actually is a table. Numbers and strings are actually tables, yet you can just use them like they were numbers and strings.

Tables can be used when:

  • You want to use the same action on some variables
  • You want to store variables
  • You want to make a handy way to store data

A normal variable is normally a "number" (such as 6 or 3.1) or a "string" (such as "Hello!" and "ugh"). A table is a list. You create tables either list-style or record-style:

-- list style
Seasons = {"Winter", "Spring", "Summer", "Autumn"}
-- record style
PlayerList = {
Banned = {jobro13 = true, ConnorVIII = false},
GiveSword = {ConnorVIII = true}
}

As you can see you can also put tables inside tables!

To get a field from a table use . for "keys" (the place in the table where they are stored at) which dont have spaces or special characters or [] for keys which have spaces or special characters.

In fact, "game" is also a "table-like" object. If you use the type function on it, it will return "userdata". Roblox (and actually, Lua) uses C as backend language - it's programmed in there. Lua basically runs in C. Userdata is data from C, but which we can edit from Lua. This is quite special.

-- example: get the "spring" field:

Season = Seasons[2] -- Return the "second" field in the list, note: Seasons.2 gives a syntax error.

-- example, return if jobro13 is banned:
jobro13Banned = PlayerList.Banned.jobro13
-- OR - exactly the same as above:
jobro13Banned = PlayerList["Banned"]["jobro13"]


4 comments

3

When do you use i,v stuff :)

3

i = index (key), v = value. Most users use this to "traverse" ("loop over") a whole table and "do things". Small example, print the contents of a table: MyTable = {3,4,5, Hello = "Hi!"} for i,v in pairs(MyTable) do print("The table MyTable has the following value: "..v.." on the index: "..i.."!") end

3

tanks bro :)

2

Thanks alot jobro, completely making my answer useless. ;3\

A table is good to use when you have to store multiple variables. Variables can only store one value, but a table can store infinite(I may be wrong here). To make a table simply type: tableName = {"variableOne",2,true} a table can store any type of value you wish to keep track of. To reference a part of a table type: tableName[1] and that will get you the first item, in this case: "variableOne". If you would like to learn more about tables, please visit the wiki: http://wiki.roblox.com/index.php?title=Table I hope this helps.

3 comments

2

Thankyou alot for this :) It actually helped me. Can I ask why you write ,2,true ? Do you write that after every part of data you store?

1

No, I was simply showing that you can store strings ("variableOne") integers (2) and boolean (true) of course, there is alot more you can store in there, including functions. :) Have a nice day.

2

Thankyou :) Can the bool and int be called upon later on my saying the table name?

This discussion is preserved as it was. New answers, comments, and votes are closed.