generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Tables

Previous Thread :: Next Thread 
sgtbubbles8 is not online. sgtbubbles8
Joined: 14 Feb 2012
Total Posts: 2231
03 Apr 2013 05:12 PM
Hi, I'm currently learning to script, and it has being going well until I came across tables...I've read up on them, but I still don't understand it, can anyone explain to me what they do, and how they're used? If you could It would be very helpful
Report Abuse
MrChubbs is not online. MrChubbs
Joined: 14 Oct 2010
Total Posts: 4969
03 Apr 2013 05:16 PM
TableOfValues = {1, 2, 3}
print("TableOfValues has "..#TableOfValues.." values in it")

print("The first value is "..TableOfValues[1])

print("The last value is "..TableOfValues[#TableOfValues])

Output:
3
1
3

Easy as pi.
Report Abuse
sgtbubbles8 is not online. sgtbubbles8
Joined: 14 Feb 2012
Total Posts: 2231
03 Apr 2013 05:18 PM
Err....
Report Abuse
notsopwnedg is not online. notsopwnedg
Joined: 07 Nov 2010
Total Posts: 4182
03 Apr 2013 05:19 PM
A table is something like this:

Table = {"One","Two","Three"}

they are used to store multiple values. They could also hold objects:

Objects = {Workspace.Part1,Workspace.Base}

They objects can be accessed bye using the Index. in the first table the string value "one" would be the first index. so i could access it by:
print(Table[1])

You could also assign keys to objects in the table like so:
Table = {
"Part" = Workspace.Part
}

and access the part by doing:
Table["Part"]


you can loop though the table using a for loop and an iterator.

for Index,Value in next,Table do
print(Value.Name,"Is At Index",Index")
end

Table would be the table you wish to loop through. Index is the current index that it is looping on. and Value is the value in the table that it is looping through.

a method like Workspace:GetChildren() returns a table that would look something like this:

Children = {Workspace.Part1,Workspace.Part2,Workspace.Part3}
Report Abuse
Azarth is not online. Azarth
Joined: 17 Aug 2012
Total Posts: 2760
03 Apr 2013 05:22 PM
Tables hold more than 1 variable, their useful when we want to check, change, get etc.. multiple things.

I wrote this to remove a list of items that have the classnames stated in "banned". I wrote it with a table because this way I don't have to use multiple if statements to remove individual items.

local banned = {"Hat", "Shirt", "Pants"}

game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function()
repeat wait() until player.Character:findFirstChild("Body Colors")
for _,v in pairs(player.Character:GetChildren()) do --Makes a table of everything in my character
for i = 1, #banned do --Goes through the banned table 3 times
if v:IsA(banned[i]) --then if anything in my character matches the classname within the banned table.
v:Destroy() --It does destroy it.
wait()
end
end
end
end)
end)


Report Abuse
josh50000 is not online. josh50000
Joined: 29 Nov 2009
Total Posts: 697
03 Apr 2013 05:29 PM
Tables are mainly useful for storing large amounts of data and can be set to a variable. For example if you wanted to store a list of data such as strings, you could set each string to a variable or set all of them to one variable using a table.
string1 = "s1"
string2 = "s2"
string3 = "s3"

OR

strings = {"s1","s2","s3"}

This saves data storage space(I think) and makes it possible for the script to add additional data using table.insert(strings,"s4"). That is one of the thing tables are useful for.
Report Abuse
sgtbubbles8 is not online. sgtbubbles8
Joined: 14 Feb 2012
Total Posts: 2231
03 Apr 2013 05:38 PM
Wow thx this deffinatly helped a ton
Report Abuse
nate890 is not online. nate890
Joined: 22 Nov 2008
Total Posts: 21686
03 Apr 2013 05:55 PM
Breif explaination... A table is, straightforwardly, a table of values. Tables are used to hold information-just like a simple variable has a value (i.e. var = 5, value is 5), a table holds a set of values. This information can range from a number of things, not limited to: strings, numbers, userdata and even functions.

Here is how a table is set up:

local myTable = {}

We put data into tables like such, separating values by comas:
local t = {3, "Hello, world!", workspace, 4, ["func"] = function() print("Hello, world!") end}
( The above function (func) is callable by doing t.func() )

We can assign specific "names" (strings) or indexes (integers) to tables, simply:
local t = {[5] = "Hello", [3] = 1, ["World"] = "!"}

We index these values by calling them by their index or their name, like so:
local t = {"STRING", [5] = "Hello", [3] = 1, ["World"] = "!"}
print(t[1], t[5], t.World, t["World"]) --> STRING  Hello  !  ! (these are case sensitive!)

We can change values in tables as well, by doing t["World"] = "Bye"-

We can index multiple values in a table by using the generic for loop,

local t = {"Hello", "world", "!"}

for index, value in pairs(t) do
  print(index, value)
end

>1  Hello
>2  world
>3  !

There are many functions associated with tables, they can be viewed here: http://www.lua.org/manual/5.2/manual.html#pdf-table.concat

Some important functions that you should note are table.remove, table.insert, table.concat and table.unpack (unpack) - table.sort is neat as well, but isn't used very often.

Adding values to tables:

There are 2 ways to do this, setting a index of the table or using the table.insert function,

t = {}

t[1] = "Hello"
table.insert(t, " World!") --
(t will appear to be {"Hello", " World!"})

There is another parameter to this function, allowing us to set our own index- table.insert(t, 1, " No"), 1 being the index.

Using the unpack function (or even table.concat), we can check the data in our table:
print(unpack(t)) --print(table.concat(t, ", "))

Removing values from tables:

To do this, we use the table.remove function.

local t = {"a", "b", "c"}

--table.remove(table, index)
table.remove(t, 2)

print(unpack(t)) --> a  c

We can only remove indexes using table.remove, but we can make our own function that handles strings, but I won't get that much in depth.

Ending this off with table.sort,

table.sort simply sorts values in a table, alphabetically or numerically depending on the userdata inside the table is (strings or numbers). However, it does not sort both strings and numbers, to do this we would need to write a function that handles but strings and numbers seperately-but again, won't be getting that in depth. Using table.sort to sort tables with both strings and numbers in them will cause the code to error.

t = {"b", "c", "a"}
table.sort(t)
print(unpack(t)) --> a  b  c

This is quite a bundle of information-though it isn't as specific as you'd find on the roblox wiki (wiki.roblox.com), however, it should be enough to start using tables in your code!

Warning: I haven't coded in quite some time (like half a year), been very busy lately.

Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image