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?
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.
1 | local t = { } |
Now we just put anything we want inside of those brackets - separated by commas.
1 | 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:
1 | local t = { "Hello, world!" , --Key: 1 |
2 | workspace.Part, --Key: 2 |
3 | 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.
1 | print ( t [ 1 ] ) |
2 | --> Hello, world! |
There's also ways where we can edit what key corresponds with what value. This video covers it pretty well.