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

Tables [closed]

Asked by 10 years ago

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

Locked by adark and Articulating

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

2 answers

Log in to vote
11
Answered by
jobro13 980 Moderation Voter
10 years ago

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"]


3
When do you use i,v stuff :) ConnorVIII 448 — 10y
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 jobro13 980 — 10y
3
tanks bro :) ConnorVIII 448 — 10y
2
Thanks alot jobro, completely making my answer useless. ;3\ Trewier 146 — 10y
Ad
Log in to vote
5
Answered by
Trewier 146
10 years ago

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.

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? ConnorVIII 448 — 10y
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. Trewier 146 — 10y
2
Thankyou :) Can the bool and int be called upon later on my saying the table name? ConnorVIII 448 — 10y