I am trying to figure out what tables are and when I use them and what I use them for can anyone help me?
What is a Table?
A Table is, well, a Table
that which contains (or stores) specific Data (As for Example: Numbers, Strings, Instances, Tables themselves, Functions (when done correctly), and Brackets (Or Variables, but used differently), ect.).
How to use a Table?
As said, a Table
is used to contain (or store) specific data, as for example, if you want to store a few letters of the Alphabet, you would beable to store a few of the letters like this:
local Alphabet = {"A","a","B","b","C","c","D","d"} --This table is containing the letters 'Aa', 'Bb', 'Cc', 'Dd' (from the Alphabet) within the Table, which can be later on used for specific tasks or purposes
How to use the Table
A Table
can be very easy to use when known how to, and can be set up to perform specific tasks (Example: From the past information), as for example, we are able to call specific Data (or Information) from the Table
(Example: Again, from the past information), but only, getting the Data from the Table
directly: There are multiple ways of doing this, but I'm only to name of few ways:
local Table = {"I","contain","strings","!",":D"} --Here we have our Table, now let's put it to use! :D print(Table[1],Table[2],Table[3]..Table[4],Table[5]) --This will print out all the Positions within the current Table; 'I contain strings! :D'; The 'print' Function will print specific Data into the 'Output', as shown above with the Table local String = ""; --The current Variable 'String' is specifying a Blank 'String' which will be used for the Following; for i,v in pairs(Table) do --The 'for' loop is used to loop through a Table, or as the ROBLOX Wiki says: 'The for loop is a way of running a command or set of commands a set number of times. The basic syntax is as following: for iterator_variable = start value, end value, increment do' String = String .. v .. " " --This will Update (or change) the Variable 'String' to reflect the Data that is being added from the Table (Example: It could be currently 'I', but then the Table will add 'contain', 'strings', and so on ('I', 'I contain', 'I contain strings', ect.')) end --This ends the Chunk local Table2 = { ['Str1'] = "I'm "; ['Str2'] = "in "; ['Str3'] = "a Table"; ['Str4'] = " with"; ['Str5'] = "Brackets!" ['Str6'] = " :D" } --I created a new Table 'Table2' to reflect upon information I stated before. The Variable 'Table2' is specifying the Table container the Data (or information). print(Table2['Str1']..Table2['Str2']..Table2['Str3']..Table2['Str4']..Table2['Str5']..Table2['Str6']) --And so, just like the last example, this will print into the 'Output': 'I'm in a Table with Brackets! :D', but, many scripters would consider this to be 'messy coding', so I wouldn't recommend doing this
How can you add data?
That's simple, but sometimes can be challenging sometimes how you use it, but, there is a way to add Data to a Already-Created Table by using the Table.insert
function: There are 3 Arguments to the function:
--1st Argument. What Table is being Manipluation, or where the Data is being stored --2nd Argument. What Data is being added to the Table --3rd Argument. Which Position the Data will be added to (for example, Position 6, 8, or 5, even those only 3 positions are filled (Table = {1,2,3,5,6,8})), but this is opitional, meaning your not required to do this; It'll Automatically set up the Position for the Data to be added to the Table
How can you Remove Data?
This is really tricky, since in the past when I tired to use it's function, it would not remove the Information directly: You had to remove it by Position, however, I will still explain:
The Table.remove
Function is specifically used to Remove Data from a Specific Table, however, it works different then from Table.insert
, or does the exact opposite, and instead of three Arguments needed, it needs Two:
--1st Argument. The Table that the Changes are being applied to --2nd Argument. The Position within the Table that is going to be removed; You can not do 'table.remove(SomeTable,\'strings\')'; It will only return an error, you must give it a Position (Example: 'table.remove(SomeTable,3)')
And that is why Table.remove
can be very, very tricky to use. If you wish for a more in-Depth explanation of Table.remove
, just let me know. :)
And that's how to properly use a Table, and the tricks to it! :D
Be Aware
From what I've read from my Lua Programming
book for 5.2
of the language: A table can only contain 1000
Position of code (or so I've read from the book, but I'm just giving this as a heads up :) ).
Information left Out
The Output
: The Output
is a Box within the Studio that which reports back Feedback (Primarily from the 'print' function'), and returns different types such as Feedbacks from the print
Function, Errors, and so on.
The For
loop: The for
loop is, as explained earlier: " --The 'for' loop is used to loop through a Table, or as the ROBLOX Wiki says: 'The for loop is a way of running a command or set of commands a set number of times. The basic syntax is as following: for iterator_variable = start value, end value, increment do'"
Strings
: Strings
are a Variable that contains Letters or other infromation, or, as the WIKI says: 'Strings are sequences of letters , numbers, and symbols.'
Got any more Questions (about this Question specifically)? Just let me know and I'll try to explain as best I can! :D
Hope this helped!
Tables are like lists, for example all the bricks in Workspace is classified as a table. A useful table is game.Players:GetPlayers() because it gets all the players. To use tables, you need to know what a i, v in pairs() do loop is. I do not know how to explain what a i, v in pairs() loop is so I can only give you an example of it.
table1 = {"Hello", " ", "What's", " ", "Up?"} for i, v in pairs(table1) do print(v) end
And the Output would say if you run that: Hello
What's
Up?
Tables and arrays work the same way, they are a list of information. For example:
What are all the test scores? How can you compact the list?
testscores = {50,65,12,99,65} --all the scores
This list can fit numbers, strings or text, Objects, and more! You can use in pairs, ipairs, and next to look through all the values and intervals of a table. There are also functions for the table. You can learn more here and here.
Hope it helps!