|
| 06 Jan 2013 03:24 PM |
Ok, so I'm pretty new to scripting. I've got a lot of it down, but when it comes to tables, I'm that one guy in the room staring at the common object screaming "SORCERY!" So, can someone teach me how to use Tables in Roblox Lua?
What IS a table? What do they do? How can I add to a table? How can I remove from a table? How can I check if a table is empty? How can I print the inputs on a table?
I've tried LuaLearners and the RobloxWiki, I'm stilled stumped. Halp?
Also, I will have follow-up questions.
> "Even the sun sets in Paradise" ~Blueboy112 |
|
|
| Report Abuse |
|
|
|
| 06 Jan 2013 03:38 PM |
I will be constantly refreshing this page until new posts...
print("Blueboy112") |
|
|
| Report Abuse |
|
|
BruceAB12
|
  |
| Joined: 19 Jan 2012 |
| Total Posts: 3238 |
|
|
| 06 Jan 2013 03:40 PM |
A table is a data type in Lua that is useful to store multiple values, including numbers, strings, functions, more tables, and much more. It is called a table because it acts like a grid with two columns: key, and value The key column is used to find a row in the table, and the value is the value that is stored in that row. Both the key and value can be any Lua value (numbers, strings, Parts etc., and even other tables) except nil. Lua tables do not have to use either numbers, strings or tables as keys. Any combination of key types can be used. Another way to describe it is that by inputting the key, you receive the value. |
|
|
| Report Abuse |
|
|
|
| 06 Jan 2013 03:43 PM |
Ok. Can I have an example of how to use a table?
print("Blueboy112") |
|
|
| Report Abuse |
|
|
BruceAB12
|
  |
| Joined: 19 Jan 2012 |
| Total Posts: 3238 |
|
|
| 06 Jan 2013 03:45 PM |
| Okay, hold up let me think of an example. |
|
|
| Report Abuse |
|
|
BruceAB12
|
  |
| Joined: 19 Jan 2012 |
| Total Posts: 3238 |
|
|
| 06 Jan 2013 03:47 PM |
Table = {"Hello",5}
print(Table[2]) |
|
|
| Report Abuse |
|
|
|
| 06 Jan 2013 03:48 PM |
So, print(Table[2]) Would print to the output
> 5
Correct?
print("Blueboy112") |
|
|
| Report Abuse |
|
|
BruceAB12
|
  |
| Joined: 19 Jan 2012 |
| Total Posts: 3238 |
|
|
| 06 Jan 2013 03:50 PM |
Correct heres another example
local a = game.Workspace:GetChildren()
for i, v in ipairs(a) do
print(v)
end |
|
|
| Report Abuse |
|
|
BruceAB12
|
  |
| Joined: 19 Jan 2012 |
| Total Posts: 3238 |
|
|
| 06 Jan 2013 03:51 PM |
:GetChildren() is a class function (referred to as method) of Workspace, which returns a table of objects currently
|
|
|
| Report Abuse |
|
|
BruceAB12
|
  |
| Joined: 19 Jan 2012 |
| Total Posts: 3238 |
|
|
| 06 Jan 2013 04:02 PM |
| So basically that script tells you how many objects are in the workspace. |
|
|
| Report Abuse |
|
|
|
| 06 Jan 2013 04:04 PM |
And, if I wanted to add/remove to the table...?
print("Blueboy112") |
|
|
| Report Abuse |
|
|
Remynfv
|
  |
| Joined: 02 Dec 2011 |
| Total Posts: 587 |
|
|
| 06 Jan 2013 04:05 PM |
| thanks I couldn't figure out tables either... |
|
|
| Report Abuse |
|
|
BruceAB12
|
  |
| Joined: 19 Jan 2012 |
| Total Posts: 3238 |
|
|
| 06 Jan 2013 04:05 PM |
This is what i got from the output.
Camera Message Terrain Script Part |
|
|
| Report Abuse |
|
|
BruceAB12
|
  |
| Joined: 19 Jan 2012 |
| Total Posts: 3238 |
|
|
| 06 Jan 2013 04:06 PM |
| @blueboy go question hmm let me see. |
|
|
| Report Abuse |
|
|
BruceAB12
|
  |
| Joined: 19 Jan 2012 |
| Total Posts: 3238 |
|
| |
|
BruceAB12
|
  |
| Joined: 19 Jan 2012 |
| Total Posts: 3238 |
|
|
| 06 Jan 2013 04:15 PM |
a = {5, 10, 15, 20, 25}
for i = 1, #a do
print(a[i])
end
That question messed with me but heres another example i would simply just remove this
a = {5, 10, 15, 20,} -- notice no more 25
for i = 1, #a do
print(a[i])
end
to add you would do this
a = {5, 10, 15, 20, 25, 30, 35} -- notice more numbers
for i = 1, #a do
print(a[i])
end
--Or
a = {5, 10, 15, 20, 25, 30, 35} -- notice more numbers
local b = game.Workspace:GetChildren()
for i, v in ipairs(b) do
print(v)
for i = 1, #a do
print(a[i])
end
end |
|
|
| Report Abuse |
|
|
BruceAB12
|
  |
| Joined: 19 Jan 2012 |
| Total Posts: 3238 |
|
|
| 06 Jan 2013 04:16 PM |
Output
5 10 15 20 25 30 35 Message 5 10 15 20 25 30 35 Terrain 5 10 15 20 25 30 35 Script 5 10 15 20 25 30 35 Part 5 10 15 20 25 30 35 Tables 5 10 15 20 25 30 35 |
|
|
| Report Abuse |
|
|
|
| 06 Jan 2013 04:18 PM |
Ok. Also, what are pairs and ipairs, if they have anything to do with tables.
print("Blueboy112") |
|
|
| Report Abuse |
|
|
BruceAB12
|
  |
| Joined: 19 Jan 2012 |
| Total Posts: 3238 |
|
|
| 06 Jan 2013 04:20 PM |
Or you can do this
a = {5, 10, 15, 20, 25}
for i = 1, #a do
print(a[i])
end
wait()
local a = game.Workspace:GetChildren()
for i, v in ipairs(a) do
print(v)
end |
|
|
| Report Abuse |
|
|
BruceAB12
|
  |
| Joined: 19 Jan 2012 |
| Total Posts: 3238 |
|
|
| 06 Jan 2013 04:23 PM |
local a = {}
a.Age = 15; a.Weight = 120;
for k, v in pairs(a) do
print(v)
end
You make a local table called a (you can make local tables :D), and then you make string indexes called Age, and
Weight. Age is 15, Weight is 120. Now, we use the pairs() iterator to go through that table. k and v are variables.
k holds the current index, and v holds the current value. So, for the variable k, and the variable v, in the
iterator function pairs, which will start unpacking the table a, print the value of the indexes Age and Weight.
pairs() and ipairs() are like unpack(), but unpack() isn't an iterator. unpack() takes an entire table and just
takes all the values out right there, and I don't know what would happen if you tried using that as an iterator
function. Both pairs and ipairs just start going through the table, and then passing the index and values to the
variables k and v. Now, k and v can be anything, it doesn't have to be k and v. You could make them j and l, as
long as you remember that you have to use those for the code body when interacting with the index and values. |
|
|
| Report Abuse |
|
|
BruceAB12
|
  |
| Joined: 19 Jan 2012 |
| Total Posts: 3238 |
|
| |
|
1Topcop
|
  |
| Joined: 09 Jun 2009 |
| Total Posts: 6635 |
|
|
| 06 Jan 2013 04:47 PM |
@Bruce you're leaving out the built in table functions, http://wiki.roblox.com/index.php/Table_Manipulation
array = {"Hi,","my name","is..."} print(table.concat(array," ")) -- table.concat connects a table, and uses the second argument (which is a string) in between each value
> Hi, my name is...
table.insert(array,"better then yours!") -- table.insert inserts a value into the array (if you put table.insert(tableName,"Index",Value) you can give the value a manual index) print(table.concat(array," ")) > Hi, my name is... better then yours!
print(#array) -- The # operator in front of the table variable will return how many number indexed values are in the array.
table.remove(array,#table) -- table.remove(tableName,index) removes the value in tableName that's at index print(table.concat(array," ")) > Hi, my name is...
array["Hi"] = "Hi, I'm a manually indexed value in my table father, his name is array. He calls me with Hi o:" print(array["Hi"]) > The string that I wrote, and was too lazy to retype xD |
|
|
| Report Abuse |
|
|
|
| 06 Jan 2013 04:47 PM |
Or a much more used way of adding/removing to Tables, which has more Usability,
table.insert(NameOfTable,TheValue) table.remove(NameOfTable,TheValue)
|
|
|
| Report Abuse |
|
|
| |
|
1Topcop
|
  |
| Joined: 09 Jun 2009 |
| Total Posts: 6635 |
|
|
| 06 Jan 2013 04:51 PM |
And you messed something up too. It's not table.remove(table,value) it's table.remove(table,index) Otherwise if you had something like,
Stats = {["Money"] = 30,["Hair Sales"] = 30} If you wanted to remove Hair Sales, and were to use table.remove(Stats,30) It would probably get rid of Money. (It won't since you have nothing indexed as 30) So it's table.remove(table,index) |
|
|
| Report Abuse |
|
|