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

How do I get better at using tables?

Asked by
SuperFryX 130
9 years ago

I've been scripting for a while now, and I've never learned how to use tables. I really want to learn how to use tables, but I find the wiki article on them to be hard to understand. How do I go about learning how to use them? And what are some ways I could include tables in my own scripts?

1 answer

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

A table is a list. This list can contain absolutely anything. To create it, we just use a regular variable name, followed by curly brackets.

local t = {}

Now we just put anything we want inside of those brackets - separated by commas.

local t = {"Hello, world!", workspace.Part, true}

Notice how we can put all types of values in the table.

We can add values, remove values, etc. with table manipulation functions.

To access a value from a table, we need to use something called a key. The key is what you use to get the value. For simple lists, the keys are numbers:

local t = {"Hello, world!", --Key: 1
        workspace.Part, --Key: 2
        true}  --Key: 3

To use a key, we put brackets after the table's name, followed by the key. This gives us the value corresponding to the given key.

print( t[1] )
--> Hello, world!

There's also ways where we can edit what key corresponds with what value. This video covers it pretty well.

0
Thanks for clearing some things up! But what would be an example of where I would use this though? I really want to implement this into my scripting but I have no idea when I would use it and what situations it would be for. SuperFryX 130 — 9y
0
That's where creativity comes in. You can use tables for all sorts of things. You could make a list of admins, or vips. Or maybe a list of maps. You could also make a list of all players still alive, or all the dead ones. There's also methods that return tables -- like GetChildren, which is very useful. The better you are a table manipulation, the more useful they become. Perci1 4988 — 9y
Ad

Answer this question