Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

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

Asked by 2 months 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
gioni01 2015 Moderation Voter Community Moderator
2 months ago
Edited 2 months 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 164 — 2mo
1
That is the same thing as indexing. User#25115 0 — 2mo
0
thank u phle gioni01 2015 — 2mo
Ad
Log in to vote
1
Answered by 2 months 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