Learn how to create tables.

by Goomiin

Author Avatar

Tables and how to use

Before using tables, you need to know what they are used for. Think of it like a backpack. The "backpack" can hold any information you want. You can put: integers, strings, booleans and other things. Example:

str = "I like cats."
int = 6
bool = true

myTable = {"Hey", bool, 2, str, false, int} -- It doesn't need to be in a specified order.

In this case, "myTable" is containing 6 informations:

Printing tables

So, at some point, when developing a game, you will need to print a table's data to see how it is working. If you try to run this command:


It won't work. Instead of printing the informations we created to the table, this command will print the inner workings of the table, thus not printing the items we wanted for it to print. Some of the correct ways to print a table:

str = "I like cats."
int = 6
bool = true

myTable = {"Hey", bool, 2, str, false, int}

print(myTable[1]) -- This will print "Hey".
print(myTable[4]) -- This will print str, which prints "I like cats.", due to the assignment.

print(#myTable) --[[ This will get the quantity of items that the table has, and print this number.
In this case, 6. When the machine is counting the quantity of the items in the table to print it, 
it will start on 1 rather than 0. ]]--

So, that's it for basics on tables. There's also iterating and functions inside tables that are a bit complicated. I wanted to keep this simple, so here you go. Basics on tables.

View in-game to comment, award, and more!