This is my third tutorial on this site. All you really need to know about is variables and basic data types (numbers, strings, etc.) to understand this tutorial.
First things first, what exactly are tables? These aren't tables that you eat and do homework on, tables in programming are used for storing information. Another word you might use when talking about tables is arrays.
The first thing you should learn is how to create a table. It's actually very simple, you just use a set of braces:
- Code:
-
{ }
Yeah, that's what those squiggly things are called. Now, like I said, tables store data and information, so let's put some data in our table. We'll give it two numbers; 3 and 7.
- Code:
-
{ 3, 7 }
See how I seperated each value with a comma? That's how you set up a table. Now, usually, you're going to set a variable to your table, so let's name our table x.
- Code:
-
x = {3,7}
Now, let's try and retrieve some of the information in that table.
- Code:
-
print(x[3])
That will print nil. Why, you may ask? Well, even though we have the value of 3 in our table, we can't find it like that, because you use the index to find things in tables. Tables are set up like this:
- Code:
-
{ Index = Value,
Index = Value,
Index = Value,
etc. }
The indices (plural for index) is how you find them in the table. But now you may be wondering how to find our value in table x, right? We've assigned 3 and 7 as values, and we haven't set any indices to be able to find them with. Right?
Well, in a way, that could be correct, but too bad. That's wrong.
When you don't manually set indices, they automatically become numbers that correspond with the position something is in the table. For example, since 3 is the first value in table x, it has the index of 1. So now, we'll use brackets (the box things) to find the first value of x.
- Code:
-
print(x[1])
That'll give us 3 in the output. Since 7 is the second value, you would obviously use x[2] to find it.
Tables in Lua can hold any type of value, and they can be assigned to indices of either numbers or strings.
- Code:
-
y = {"Forty-two", Seven = 42}
In that example, we made variable y, and set it to a table, with index 1 being the string "Forty-two", and index Seven (not 7) being the number 42. Now, if we print y[1], it gives us Forty two in the output.
But now, if we print y[2], it will give us 'nil'. Why do you think that is? well, we gave the second value a string index, so it doesn't have a numerical index anymore.
To access string indices, you can do it in two ways. Either the same way that numeric indices are found:
- Code:
-
print(y["Seven"])
Or using a period, just like if you were to go down the hierarchy of the game (game.Workspace.Part).
- Code:
-
print(y.Seven)
Both of those examples would print 42.
You can also put other types of data as your values, such as functions, objects, or even other tables :O
Here's a slightly more complex table:
- Code:
-
Table1 = {"Potatoes",
urmom = {Pie = "Insert potato here", 51},
lolwuttt = "HAI"}
Now, see if you can figure out what each of these would print (note, some might print nil, so don't assume they have to be correct :P):
- Code:
-
print( Table1.lolwuttt )
print( Table1[1])
print( Table1.urmom[2] )
print( Table1.Pie )
If you can figure all of them out without actually running the code, then good for you ^_^. Under this spoiler is the answers to what each would print:
- Spoiler:
print( Table1.lolwuttt )
This would print "HAI"
print( Table1[1])
This would print "Potatoes"
print( Table1.urmom[2] )
This would print 51
print( Table1.Pie )
This would print nil
If you have any questions, comments, feedback, or cookies, then leave a comment below :D