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

How do tables work? (And problems with StackEdit)

Asked by
sheepposu 561 Moderation Voter
5 years ago
Edited by Amiaa16 5 years ago

I've heard of tables and I've seen them, but I don't understand them. Can someone tell me what are they used for? How do u use them? Could someone also give me an example. Thx in advance. Also I had to put in a link because StackEdit was closing 1 second after I opened it. It took forever just to get the link in there. Another thing. After not being able to open StackEdit I went to inspect and found it giving me these errors when I tried to open it:

Failed to load resource: the server responded with a status of 404 ()

&

Uncaught (in promise) TypeError: Cannot read property 'postMessage' of undefined
at content_script.js:32553

&

Uncaught (in promise) Error: csPostMessage: timeout 60000 ms
at content_script.js:32518
(anonymous) @ content_script.js:32518

&

Uncaught (in promise) TypeError: Cannot read property 'postMessage' of undefined
at VM3475 content_script.js:32553
(anonymous) @ VM3475 content_script.js:32553
Promise.then (async)
onMsg @ VM3475 content_script.js:32552

The first time I opened it gave the 2nd and 3rd error. Then the 4th error kept piling up every time I tried to open it again. Anyway hope that helps fix StackEdit, or if I need to fix something.

0
good thing roblox has the answer for you [https://developer.roblox.com/articles/Table](https://developer.roblox.com/articles/Table) ice_bearz 36 — 5y
0
Edit your question to ask your question here. A question which is just two links is going to be removed. BlueTaslem 18071 — 5y
0
Put your question, not in pastebin. That's the entire point of this. CaptainAlien132 225 — 5y
0
I put it in pastebin bin because I can't open StackEdits sheepposu 561 — 5y
View all comments (3 more)
0
StackEdits will only open for 1 sec, then close sheepposu 561 — 5y
0
Why will you need a pastebin if your question is about an explanation? starmaq 1290 — 5y
0
IDK. I just used pastebin as a way to display what I would've displayed here sheepposu 561 — 5y

1 answer

Log in to vote
0
Answered by
starmaq 1290 Moderation Voter
5 years ago
Edited 5 years ago

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

--a table is assigned this way
t = {}
--that's a table, "t" is its name and those 2 brackets thingies will have the values inside them, and values are seperated with a ,
--so let's try inserting some values
t = {128, "hi", game.Workspace.Part, true}
--see, we inserted many values, we can put in it numbers
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]
--now let me explain that, first you need to type it this way, t[] the table's name and 2 blocky brackets, but you might be wondering what's index? index is an integer, you can call it the "rank" or placement of a table's member (all values inside a table are called a member of that table) so lets get back to that original table. i'll change the table's name to make it more comprehinsible.


lcoal maTable = {128, "hi", game.Workspace.Part, true}
print(maTable[2])
--if you do that that should print "hi", beacues "hi" is the second memeber. its index is 2; get the idea?
print(maTable[3]) --should print Part because its 3rd
print(maTable[1]) --should print 128 cuz its 1st

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]

--if you got long scripts, it might be easier to do it that way, instead typing it this way.

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.

--if you knew nothing about tables you would do this
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

--isn't that just unneficient, you can use tables here! but how you might ask?

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()
--you see this ^, this is a table, you might not see it as a table, but in your script, children has table inside it hidden. that's a table containing the children of that object, which is the model.

--keep an eye on this part, this is called an in pairs loop, and its used to loop through a table's memebers.

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.

--let's say we got back to one of our earlier script.

 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]

--this is the dialogue script, and we can actually make it more efficient yet again. instead of chaning the text with each line on it self we can use in pairs loops.

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

--an in pairs loop is written this way
for i, v in pairs(table) do
    --table, is the table that we're gonna loop threw its members.
    --the variable that we commonly call "v", is the member of the table that is currently getting looped. So in our earlier script. the first member will go through which "Hay!", then "How's it going?" will go, and like that.
    --the variable that we commonly call "i", is the index of the member that is currently getting looped, so if you would print the index each time it loops, it will print 1, 2, 3 and as many members the table has. at first "Hay!" will go through and its first so its index is 1, and like that.. and this is almost never used so yeah.
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!

Ad

Answer this question