~-Replace this Thread when it's supposed to be somewhere else-~
Welcome to my second tutorial/guide wich is about Tables.
~-Indexing-~
Tables are good for storing data, you create a Table by using the '{}' symbols:
- Code:
-
local tab = {}
Indexing a table is easy and can be done in around four ways:
- Code:
-
local tab = {k = "Hello World"}
or
- Code:
-
local tab = {}
table.insert(tab, k = "Hello World")
or
- Code:
-
local tab = {}
table.insert(tab, k = "Hello World", 1)
Now example1 will create a table with k = "Hello World", so if I say print(k) it will result in "Hello World".
Example2 creates an empty Table and indexes it with k = "Hello World" so afther that the Table looks like example1.
Example3 creates an empty Table too, but the given value is indexed on key 1.
~WARNING: Don't call a Table 'table' or 'Table' also don't frogot that table.insert(table, value, key) might replace the key that's aleardy been setted:
local tab = {j = "Hello World"}
print(j) --> Hello World
table.insert(tab, j = "Oh my", 1)
print(j) --> Oh my ~
_________________________________________________________________________________________________
~- The use of for loops in a Table -~
For loops are very usefull at Tables:
- Code:
-
local names = {}
script.Parent.Touched:connect(function(p)
local h = p.Parent:findFirstChild("Humanoid")
if h then
for _,v in pairs (names) do
if h.Parent.Name == v then
print("Aleardy touched")
elseif h.Parent.Name ~= v then
table.insert(names, h.Parent.Name)
end
end
end
end)
So that creates a Table called "names".
Line 2, 3 and 4 are obvious.
Line 5: A for loop is created with v == all values in the Table "names"
Line 6: Checks if h.Parent's Name is equal to all the values in the Table names.
Line 7: If the if statement returns true then it prints "Aleardy touched".
Line 8: Else h.Parent's Name ISN'T equal to all the values stored in the Table "names" then
Line 9: If the elseif statement returns true then it inserts h.Parent's Name into the Table "names"
Line 10-13 are all the ends.
________________________________________________________________________________________________
There are many Table functions, such ash table.insert, table.insert, table.concat etc read more about these functions in the Wiki:
http://wiki.roblox.com/index.php/Function_Dump/Table_ManipulationDon't like this Thread? Well then read the Wiki:
http://wiki.roblox.com/index.php/Table