Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

What is the best way to learn tables?

Asked by
Relatch 550 Moderation Voter
9 years ago

I would like to learn how to make a table. Tables are very important for things I want to make, so this could be very helpful. I've tried the wiki, but couldn't find it. If there is anything else other than the wiki, please send me a link or a few lines of code which include making a table, and how to remove things(such as players) from it. I say other than the wiki, because it's very hard to understand.

0
To add on to Perci's answer; the best way to learn *anything* in a programming language is to experiment with it. Look at the reference documents to see what methods and functions you have to work with, and just play around with them. Looking at working code examples is also a good idea. *Every* major game in ROBLOX uses tables in some way. adark 5487 — 9y

1 answer

Log in to vote
2
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

This wiki documentation is here, and this is a helpful video on the subject. I'll also explain a bit on it myself.


Tables in Lua are just like your dining table in real life. Your table holds stuff. Your table will hold anything you put on it. It's the same in Lua. To create a table with scripting, we make a regular variable name followed by curly brackets.

local myTable = { } --this table is empty

Now we just put stuff in it, whether that's an object, number, string, boolean etc.

local myTable = {4, "Hello", true, workspace.Part}

To access a value, we just count. 4 is 1, Hello is 2, true is 3, and so on. This number is called the key and the actual thing in the table is called the value. The key is used to access the value. For example, you use a key to access your house. The house is the thing you want, the key is just how you get there.

We then just put the correct number (or the correct key) in brackets after the table name:

print( myTable[2] )
--> Hello

There's also ways you can change the key you want to use to get the value instead of it just being a number, but we won't go into that.


Here's some nice table manipulation functions you can learn.

0
Suggestion: Explain what datatypes can be placed into a table(: Goulstem 8144 — 9y
0
Didn't I? "Now we just put stuff in it, whether that's an object, number, string, boolean etc." Perci1 4988 — 9y
Ad

Answer this question