Now tables, also known as arrays are also a sort of variable that can store things, but tables store many values unlike normal variables.
Basics
t = {}
t = {128, "hi", game.Workspace.Part, true}
integers, instances (like the pathway of a part or any object), bolleans, and a lot. And of course a table can be local (you can add a local next to it).
And now, you may be like: how to refer to the values that are inside that table?
Well, you do it this way
t[index]
lcoal maTable = {128, "hi", game.Workspace.Part, true}
print(maTable[2])
print(maTable[3])
print(maTable[1])
And that is pretty much the absolute basics of tables!
We are about to get into the use of tables, and advanced stuff about them. but for more information, click here.
The use of tables
Now, you might be wondering when you need to use a table, it's for occations that are mostly common, and if you get used to scripting, you'd know when to use tables instanly when needed.
But, they are used when we want to make our script more efficient. For example this is rare but you can use it for a dialogue.
(put in mind that syntax can be multi-lined, so you can write it like down here)
lcoal ui = script.Parent
local lines = {
"Hay!",
"How's it going?",
"Hello World!"
}
ui.Text = lines[1]
ui.Text = lines[2]
ui.Text = lines[3]
ui.Text = "Hay!"
ui.Text = "How's it going?"
ui.Text = "Hello World!"
And now for the most common use (this will use something that you don't know yet but i'll show you it in the next chapter).
Ok, let's say you wanted to make some parts that are inside a model change their transparency.
local model = script.Parent
model.Part1.Transparency = 0.5
model.Part2.Transparency = 0.5
model.Part3.Transparency = 0.5
model.Part4.Transparency = 0.5
model.Part5.Transparency = 0.5
So, you know that certain functions return something, which means they give us something back. For example :IsA() will return true if the set as the argument class is met and false if not, it returns a boolean value in general. A really used function that we're gonna use here too is :GetChildren(). That function doesn't return a bool. It returns a table.
A table containing the children of the wanted object.
local model = script.Parent
local children = model:GetChildren()
for i, v in pairs(children) do
v.Transparency = 0.5
end
And that's the use that you will always meet.
In Pairs Loops
Now, there's still a lot of stuff about tables, but i'm gonna go threw one lat thing that is really important. InPairs Loops
As I Said eariler these are used to loop through all members of a table.
lcoal ui = script.Parent
local lines = {
"Hay!",
"How's it going?",
"Hello World!"
}
ui.Text = lines[1]
ui.Text = lines[2]
ui.Text = lines[3]
lcoal ui = script.Parent
local lines = {
"Hay!",
"How's it going?",
"Hello World!"
}
for i, v in pairs(lines) do
wait()
ui.Text = v
end
That makes it more efficient, now let me explain what I just wrote
for i, v in pairs(table) do
end
That's pretty much it!
lcoal ui = script.Parent
local lines = {
"Hay!",
"How's it going?",
"Hello World!"
}
for i, v in pairs(lines) do
--here we put "lines" as the table cuz it is the table that has our lines
wait()
--and you can see each time it loops we're setting the text of the ui as v, which in the first loop is "Hay!", then after 1 wait() it becomes "How's it going?" and so fourth
ui.Text = v
end
And that's a brief explanation on tables! There's much much more though.
I'm happy to help! And if you have an question or you didn't understand just ask!
7 comments
good thing roblox has the answer for you
https://developer.roblox.com/articles/Table
Edit your question to ask your question here. A question which is just two links is going to be removed.
Put your question, not in pastebin. That's the entire point of this.
I put it in pastebin bin because I can't open StackEdits
StackEdits will only open for 1 sec, then close
Why will you need a pastebin if your question is about an explanation?
IDK. I just used pastebin as a way to display what I would've displayed here