Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Brackets but not parentheses?

Why does the line script.Parent["Left Arm"].Anchored = false work, but when I replace the brackets [ ] with parentheses ( ), it no longer works?

2 answers

✓ Accepted answer

Brackets and parenthesis are never equivalent in Lua syntax. If they were interchangeable, it would result in ambiguity, which would make programming impossible.


script.Parent("Left Arm") syntactically works the same as print("Left Arm"): Juxtaposing parenthesis means function call which is not what you mean when you say script.Parent["Left Arm"].

Brackets, on the other hand, refer to table (+ child/property) indexing.

Because you need to be able to express both of these things, and in any & all events only one of them, parenthesis and brackets have to be separate in their meanings.



Other uses of Parenthesis / Braces / Brackets (Not directly relevant to question)

For thoroughness I'll mention the other uses of parenthesis / brackets / braces that were not mentioned by you.

Parenthesis also accomplish grouping of operations, e.g., (5 + 3) * 2 is 16 and 5 + 3 * 2 is 11.

Double square brackets are used to create multiline strings which ignore escape sequences:

local str = [[Hello
this is a long
string]]

Those are also used to make multiline comments:

--[[ I have a lot to say
about this particular
piece of code]]

Curly braces ({ and }) are used in Lua to define table literals, e.g.,

local list = {2,3,5,7,13,17,19}

2 comments

0

Thank you! This is exactly what I was looking for! Going into detail on that last part really helped also.

0

Omg I just realized I just had my question answered by the person who makes my favorite ROBLOX Portal game! o.O :D

This discussion is preserved as it was. New answers, comments, and votes are closed.