> This post will cover; Tables, For Loops, Vector3, RBXScriptSignal
# The Basics 2
## Table of Contents
- [Tables](https://hackmd.io/@Drastic/By9EuORiR#Tables)
- [Creating a Table](https://hackmd.io/@Drastic/By9EuORiR#Creating-a-Table)
- [What is an Index?](https://hackmd.io/@Drastic/By9EuORiR#What-is-an-Index?)
- [What is a Value?](https://hackmd.io/@Drastic/By9EuORiR#What-is-a-Value?)
- [Working with a Table](https://hackmd.io/@Drastic/By9EuORiR#What-is-an-Index?)
- [Table Visualized](https://hackmd.io/@Drastic/By9EuORiR#What-is-an-Index?)
- [For Loops](https://hackmd.io/@Drastic/By9EuORiR#For-Loops)
- [Vector3](https://hackmd.io/@Drastic/By9EuORiR#Vector3)
- [Creating a Vector3](https://hackmd.io/@Drastic/By9EuORiR#Creating-a-Vector3)
- [Vector3 Properties](https://hackmd.io/@Drastic/By9EuORiR#Vector3-Properties)
- [RBXScriptSignals](https://hackmd.io/@Drastic/By9EuORiR#RBXScriptSignals)
# Tables
Tables are EXTREMELY useful, you can store a ton of data inside of them and use them for basically anything
Tables are pretty tricky to understand however so i will try to demonstrate it with a literal github table
## Creating a Table
To create a table you can use the curly braces: {} this creates a new table with nothing in it. To define values into the table you can just put things into it separated by commas.
```lua
-- Blank Table
local NewTable = {}
-- Not Blank Table
local NewTable = {
"String",
10837,
true,
false
}
```
### What is an Index?
An index is the position of a value inside of a table, it can be manually defined when creating the table or when adding the value into the table. If an index is not provided then the index will just be one more than the length of the table (# +1).
```lua
-- Table with automatic indexes
local NewTable = {
"String", true, false
}
print(NewTable)
--[[
Output: {
[1] = "String",
[2] = true,
[3] = false
}
]]
-- Table with manual indexes
local NewTable = {
[-1] = "String",
["StringIndex"] = true,
}
-- NOTE: i dont recommend having tables with multiple DataTypes as indexes. this is just an example.
print(NewTable)
--[[
Output: {
[-1] = "String",
["StringIndex"] = true
}
]]
-- This will also work for string indexes
local NewTable = {
Value1 = "hi",
Value2 = 123
}
```
### What is a Value?
A value is just whatever is given to the table that you want to store.
## Working with a Table
When working with a table you will treat them the same way as an instance property where you can use the `dot notation` to access and change their values.
to access a specific value you will access the table and type whatever index is assigned to that value
```lua
local StringVar = "Value2"
local NewTable = {
Value1 = "hi",
Value2 = {
1, 2, 3
}
}
-- If i want to read Value2 i can do it like this
print(NewTable.Value2)
print(NewTable[StringVar]) -- same thing as above but with a variable
-- I can Change Value2 like this
NewTable.Value2 = "Hello i am not a table anymore"
print(NewTable.Value2) -- Output: Hello i am not a table anymore
-- To create a new value i can do this
NewTable.ThisIsACustomIndex = "Hi"
-- this will do a automatic index
-- i will do a tutorial on the table global soon
table.insert(NewTable, "Hi")
```
---
### Table Visualized
```lua
local table = {}
table.x = "foo"
-- x is our index and is that we can use to grab "foo" from our table
table.y = "boo"
```
| Index | Value |
| -------- | -------- |
| x | "foo" |
| y | "boo" |
but now what if we try to get rid of our value?
```lua
local table = {
x = "foo",
y = "boo"
}
table.y = nil
-- gets rid of "boo"
-- however the index y still stays until it gets garbage collected
-- {
-- x = "foo",
-- y = nil
-- }
```
# For Loops
Now that you understand tables, i can now show you what a for loop is.
A for loop can be used in a few different ways, it has a use for tables to run code on all the tables contents and it can also be used to run a chunk of code a specific number of times. **Loops arent the same as functions**
```lua
-- Table
local NewTable = {"i am a string", "i am also a string"}
for Index, Value in NewTable do
print(Index, Value)
end
-- Repeating # of times
--[[
this version takes 3 arguements, starting number, ending number, interpolater
what this will do is that it will start with an Index
that is equivalent to the starting number and add the interpolater
to that number until it reaches or exceeds the ending number.
each time the Index is changed it will run any code inside the loop.
NOTE: the 3rd arguement isnt required
]]
for Index = 0, 10, 1 do
print(Index)
end
-- Output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
-- Any of the arguements can be negative, for example:
for Index = 10, 0, -1 do
print(Index)
end
-- Output: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
```
# Vector3
A Vector3 represents 3 numbers that are used to determine an objects position in a 3d space. it uses 3 numbers: XYZ. They are used to determine the position of things like a part.
## Creating a Vector3
Vector3 can be created using `Vector3.new()`, it takes 3 number arguements.
```lua
Vector3.new(1,2,3)
```
## Vector3 Properties
When you think of a vector3's properties you can imagine them like a table
```lua
Vector3.new(1,2,3) -> {
X = 1,
Y = 2,
Z = 3
}
```
You can read these properties just like you can with a table, it however is not a table so you cant do things like for loops to them.
```lua
local Vector = Vector3.new(1,2,3)
print(Vector.X)
```
# RBXScriptSignals
an RBXScriptSignal is basically an event that other code can listen for or fire.
## Why are they useful?
## How to use it
### Creating RBXScriptSignals
- **BindableEvent**
To Create a BindableEvent you can use `Instance.new()` like this
```lua
local BindableEvent = Instance.new("BindableEvent")
```
To connect events onto our created BindableEvent we can use `BindableEvent.Event` and connect to it.
```lua
local BindableEvent = Instance.new("BindableEvent")
--The Event is the internal RBXScriptSignal that we can connect to.
local Event = BindableEvent.Event
local Connection = Event:Connect(function()
--Code here will run if we call :Fire() on the BindableEvent
print("BindableEvent was fired")
end)
BindableEvent:Fire()
```
**BindableFunction**
```lua
local BindableFunction = Insta
```
### Using Built in RBXScriptSignals
## API Reference
### BindableEvent
```lua
:Wait() -- Waits until the RBXScriptSignal is fired.
:Connect(fn: function) -- Runs fn when the RBXScriptSignal is fired
:Once(fn: function) -- Same as :Connect() but can only run once
:Fire(args: ...any) -- Fires the Signal with the arguements provided
```
```lua
local BindableEvent = Instance.new("BindableEvent")
local RBXScriptSignal = BindableEvent.Event
RBXScriptSignal:Connect(function(...)
print(...)
end)
-- to fire the RBXScriptSignal do
BindableEvent:Fire(...)
```