Ad
Log in to vote
0

What do [] brackets do and the other ones if my understanding is wrong?

Asked by 4 months ago

I have seen all kinds of brackets these : {} I think are for tables these () I think are for parameters . BUT THESE [] I cannot understand. I don’t get what [] bracket do.

0
I'm not sure on the proper reason as to why but I have seen them used, and used them in tables for callable number names for tables, alongside getting to the child of an instance if that instance has a name with a space. I.E workspace["Roblox Headquaters"] Protogen_Dev 268 — 4mo
0
You can use it for either finding a child or getting a poperty of an instance. SuperPuiu 482 — 4mo

4 answers

Log in to vote
0
Answered by
MattVSNNL 614 Moderation Voter
4 months ago

A lot of times it's used to get certain stuff out of tables or dictionary's, but it can also be used to find a child inside of Workspace or another place, It can find children with spaces

Dictionary example: (Tables and Dictionary's are almost the same}

-- Make the dictionary using the [] brackets to name them!
local fruitDictionary = {
    ["Bananas"] = "Not my favorite";
    ["Apples"] = "Pretty good";
    ["Eggplants"] = "Sus";
}

-- Print the Eggplants
print(fruitDictionary ["Eggplants"])
-- When you run the script it should say sus!

Children Example

workspace["My Part that has a space"].BrickColor = BrickColor.new("Bright green")

Hope this answered your question!

Ad
Log in to vote
1
Answered by
bbissell 346 Moderation Voter
4 months ago

Square brackets are used to reference items in a table. For example:

local table = {12, 41, 62}

print(table[2])  --prints 41

table[2] = 123

print(table[2])  --prints 123

print(table[1])  --prints 12

They can also be used to reference table keys that are strings:

local table = {
    ["oranges"] = 152,
    ["apples"] = 127,
    ["potatoes"] = 4
}

print(table["apples"])  --prints 127
Log in to vote
0
Answered by
Robb12 12
4 months ago

Square brackets can be used to find things you may not be able to without using them. another use is for picking a certain item in a table

part = workspace.my part -- This will not work
part = workspace["my part"] -- this will work

mytable = {one, two, three}

mytable[2] = "seven"

I hope this makes sense

Log in to vote
0
Answered by
enes223 327 Moderation Voter
4 months ago

hey you! have you ever heard of enes? if you are in trouble, better call enes!

Answer this question