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 » Scripters
Home Search
 

Re: Beginner's guide to tables

Previous Thread :: Next Thread 
maxomega3 is not online. maxomega3
Joined: 11 Jun 2010
Total Posts: 10668
29 Mar 2015 12:19 PM
Tables. What are they?
A table is similar to a variable, but instead of holding one value, it can hold many different values.

*Defining a Table*
Defining a table is not much different than defining a variable. The only difference is that you can call an empty table by using {}.
Ex:
local ThisIsAnEmptyTable = {}

To put things in a table, you just list multiple values. Don't forget to separate each value with a comma (,) or a semicolon (;) and encase the table in those brackets ({}).

Here's what that might look like:
local exampleTable = {42, "Hello World!", game.Workspace, game.Teams:GetChildren ()}

Note how in the table above, any type of data can be in a table, whether it's a number, "string", instance, or even another table!

*Indexing a Value from a Table*
So you've got your table. Now what? Well if you want to retrieve a value from your table, what you need to do is restate your table and use square brackets ([]) with a number inside of them. That number is what place the value is in on the table. Let's take a look at the exampleTable again:

local exampleTable = {42, "Hello World!", game.Workspace, game.Teams:GetChildren ()}

42 is first on the list, and game.Teams:GetChildren () is 4th. To get 42, we do this:
exampleTable[1]
If you were to print that, the output would read 42!

We call what's in the brackets a "key" and the value attached as...well, the value. In this case, 1 is our key and 42 is our value.

*Dictionaries*
Well, you learned about tables already. What are dictionaries? Technically, all you've learned so far are arrays. They are a certain type of table, just like dictionaries. What's the difference? A dictionary has non-numerical keys. Just like a dictionary in the real world, a table dictionary has a series of words (keys) and definintions (values). Here's an example of a dictionary:

local Webster = { ["Maxomega3"] = "The guy helping you understand tables";
["Life"] = 42;
["Baseplate"] = game.Workspace.BasePlate;
["Function"] = function () print ("Hi") end}

If you'll notice, all the keys are denoted by [""]. That's the traditional way of defining keys, but a shortcut for keys without spaces is to just leave out that stuff (So ["Life"] would just be Life).

*Indexing Dictionary Values*
Well, indexing values from a dictionary is almost just like that in an array, except dictionaries use the non-numerical key when indexing a value. If we wanted to get the baseplate, we would do this:

Webster["Baseplate"]
or
Webster.Basplate -- given that there's no spaces in the key

*Multidimensional Tables*
I can't say I'm an expert on these, but 2D tables are similar to standard tables with an exception that you need 2 keys to get a single value. All values in a 2D table are normally paired. Don't freak out if you don't understand them at first because I can't say that I've needed to use them before in my experience; it's just a good thing to know in case you might need it.

local TwoDTable = { {x,y};
{0,1};
{"Hi","Hello"};
{math.pi,game.Lighting}
}

*Indexing a Multidimensional Table*
Like I said before, indexing a value from a multidimensional table needs two keys. One is for the position in the table (just like in an array), and the other is for the position in the pair.

TwoDTable[3][2] is "Hello" while TwoDTable[3][1] is "Hi"

*Practical Uses for Tables*
Tables are very useful in programming (as well as dining). You will likely see that you use them a lot. ModuleScripts often are just compiling neat functions into one big table. Common uses for tables are:
>Getting random elements (try using math.random (1,#table) for a key in an array)
>Keeping track of similar objects
>Shop systems (although I use string manipulation, but that's another lengthy help thread)
>Points in a path for pathfinding
>Getting the children of an instance (:GetChildren () creates a table!)
>So much more!
Report Abuse
Bitwyl is not online. Bitwyl
Joined: 15 Nov 2014
Total Posts: 7276
29 Mar 2015 12:30 PM
Super helpful, thank you.
I can easily read tables, but I wasn't sure how to retrieve information from it exactly. Thanks for taking time to make this! :)
Report Abuse
Augmentary is not online. Augmentary
Joined: 30 Aug 2013
Total Posts: 8081
29 Mar 2015 12:40 PM
This is the first table demo I approve of.
Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
29 Mar 2015 12:43 PM
Thread > wiki


I script -~ chimmihc
Report Abuse
helicopter35 is not online. helicopter35
Joined: 25 Oct 2010
Total Posts: 1225
29 Mar 2015 12:43 PM
#Tracked
Report Abuse
maxomega3 is not online. maxomega3
Joined: 11 Jun 2010
Total Posts: 10668
29 Mar 2015 12:50 PM
Thanks guys!
Report Abuse
theftblade is not online. theftblade
Joined: 28 Jun 2009
Total Posts: 18
09 May 2015 11:55 PM
So what if you have table = {1,2} how can i make it so if mouse.Target.Name == Searchtableforname then ...?
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
10 May 2015 12:33 AM
You're forgetting that keys can be anything except nil :(

local tbl = {};
tbl[true] = "Hi";
print(tbl[true]);
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • 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