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

What does putting [] and setting it to something mean?

Asked by 5 years ago

for example

I saw these kinds of scripts


playersfound = {} -- I understand this is a table --But I dont get how a table can be set to to a bool value playersfound[part.Parent.Name] = true playersfound[part.Parent.Name] = part.Parent

I don't understand what these types of things mean

1
It's a dictionary index (10/10 comment) Ziffixture 6913 — 5y
0
Lets say a part in workspace. The name has a space or symbol instead of putting workspace.Part Test you put workspace[“Part Test”] Dalbertjdplayz 37 — 5y
0
I recommend starting with this link (https://developer.roblox.com/articles/Table) and learn all you can about tables and then move on to discover something else. SteamG00B 1633 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago

Tables in general


In tables in general, the square brackets [] are used to index a table, it can be used to get and set the value attached to that index of the table

In dictionaries, square brackets are primarily used as an alternative for . when the key string contains a whitespace, such as "Hello World", or "Grape Juice".

For example:

local tbl = {}
tbl["Hello World"] = "hello there" --(sets the value attached to index)
print(tbl["Hello World"]) --hello there (gets the value attached to the index)


Roblox Objects


On the topic of roblox objects, I believe i explained it in enough detail in my answer on this question, so you should check that out for a more in depth explanation than i will be giving here.

But , in essense the square brackets , when used on objects such as a baseplate, workspace, etc. essentially get one of the properties / children / methods of said object's metatable.

You may be confused on what I mean "the object's metatable" Well, as roblox objects are all userdata values, they are attached to a metatable containing all the children, methods, and properties.

If any of that sound familar, it is because the newproxy function, when given a true parameter, returns a userdata attached to an empty metatable. newproxy is also handy for things like metatable wrapping

Hopefully this helped!

Ad
Log in to vote
0
Answered by 5 years ago

As Feahren highlighted in his comment, it is the index of a dictionary.

In Lua a table can be treated as an array (a list of values) or a dictionary (a list of keys and values).

Lets define part.Parent.Name as abc

In your case you have a table: playersFound.

The following lines say this:

Take the entry (or key) abc in the table playersFound and set it's value (which can be anything!) to be 'true'.

The next line is fairly similar:

Take the entry (or key) abc in the table playersFound and set it's value to an Object (in this case part.Parent), and we can change the value from a boolean (like true or false) to an Object because a key in a dictionary can have any kind of value.

For more information and examples see: Tables

Answer this question