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

can i ask about this bracket [ ] what its actually mean?

Asked by 5 years ago

i just wondered what this bracket means in roblox coding because all of the video that i watching for scripting tutorials didn't tell what this bracket means so if anyone answer me about this question i would highly appreciate and thank you for answering my question

2 answers

Log in to vote
3
Answered by 5 years ago
Edited 5 years ago

In Lua, brackets can indicate a couple of things:

Multi-line comments and strings

You can comment multiple lines using double brackets on the beginning and closing of your code. This is useful for temporarily disabling snippets of code in case you want to save them for later.

local part = workspace.Part

part.Touched:Connect(function(otherPart)
   print(1)
   --[[if otherPart.Name == "Test" then --this snippet will not run
      print(2)
   end]]
end)

You can do the same with strings (simply without the dashes, which indicate comments).

local str = [[
hi
how
are
you]]

Table Indexing

Single pairs of brackets are used for indexing tables, like so:

local t = {"Hello"," world","!"}

local SelectedIndex = t[1] --the first key of the table
print(SelectedIndex) --prints "Hello"

You can also create new sections in tables. I tend to do this when storing player data:

local PlayerData = {}

game.Players.PlayerAdded:Connect(function(plr)
   PlayerData[plr.Name] = {
        AccountAge = plr.AccountAge --etc
   }
end)

Resources:

Multi-line Strings

Multi-line Comments

Table Indexing

Accept and upvote if this helps!

0
u forgot things like workspace.Player[thing] HappyTimIsHim 652 — 5y
1
That is the same thing as indexing. User#25115 0 — 5y
0
thank u phle Gey4Jesus69 2705 — 5y
Ad
Log in to vote
1
Answered by 5 years ago

The brackets [] mean you are dereferencing a key in a table. In other words, go to the spot that is specified in the brackets, and get the value that is stored there.

-- Take this table as an example
tbl = {
    A = "something",
}

-- Now, we can access the data stored in 'A' by doing this:
print(tbl["A"]) -- this prints "something"

-- We can also set values in the table

tbl["B"] = "something else"

-- And we can also access this
print(tbl["B"]) -- this prints "something else" 

Answer this question