while learning how to code, I have kept seeing some people use a table without anyone inside like this:
1 | local table = { } |
i keep on wondering and thinking if that means it’s empty or there are something in it. Also i wonder on what those are used for. Any help would be great, thanks!
Yes, this is an empty table. But you should not name a table "table", because that is a special keyword on roblox. Any text that turns blue is a special keyword you should not use to name anything in your code. Tables are basically arrays or a list. They can store information like strings, booleans, functions, and more. Usually people use them to store items in a player's inventory.
01 | local myTable = { } -- Empty |
02 | table.insert(myTable, "Oranges" ) -- Inserting the string "Oranges" |
03 | print (myTable [ 1 ] ) -- Printing the first item in our table: Oranges |
04 | -- Inserting Apples, Grapes, and Bananas |
05 | table.insert(myTable, "Apples" ) |
06 | table.insert(myTable, "Grapes" ) |
07 | table.insert(myTable, "Bananas" ) |
08 | table.remove(myTable, 3 ) -- removing the 3rd item in our array/table/list: Bananas |
09 |
10 | for i, v in pairs (myTable) do -- printing every item in our list |
11 | print (i, v) |
12 | end |