5

# Tables [closed]

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.

11
jobro13 980
9 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 — 9y
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 — 9y
3
tanks bro :) ConnorVIII 448 — 9y
2
Thanks alot jobro, completely making my answer useless. ;3\ Trewier 146 — 9y