What is [] In a script

Hello i was just wondering what [] Does? I will show an example

Character[“Left Leg”]
Does it skip the Character.Blah.Blah Processes? you know the things with Dots, thats just my guess. Also if it does do that then whats the point of ever using the Dot version.

2 Likes

No, it’s another method of indexing other than dot notation (i.e. foo.bar). It is useful for names with spaces in them and for getting non-string values like the value of a variable:

local variable = "Part"
workspace[variable].Transparency = 1

If you did workspace.variable, it wouldn’t work, but [variable] substitutes it with “Part”.

3 Likes

It really doesn’t skip the process, it pretty much returns a value or object that is chosen like the example you have given: Character[“Left Leg”]

It’s a way to to get that particular index that is inside the brackets.

An example to use brackets would be like this as a metatable:

local Table = {2, 8, 11, 22}

Print(Table[1]) --Here it would mean to print the first thing in the table. :3

Another:

local globalPart = Instance.new("Part")
 
local testDictionary = {
	PartType = "Block",
	[globalPart] = true
}
1 Like

So you use it for names that have spaces? I see does the Dot version not work with spaced names?

Also whats a Non-String value?
Sorry if i’m being bothersome i’m new to this.

a.b is syntactic sugar (a nicer way to write something) for a["b"]. However using the . notation for indexing only works if the indexer is a valid identifier (i.e a legal variable name)

So this works:

local t = { v = 56 }
print(t.v)
print(t["v"])

But something like this won’t:

print(t.Cool Key)

because Cool Key is not a valid identifier. That is where [] comes in

1 Like

I think i get it.
Quick question then so you can use [] for the same things as . ? why do ppl use . then [] seems allot faster, unless i’m misunderstanding something.

Also just to make sure sure, What do you mean valid identifier? Never heard of that before. What makes something a Valid identifier, or not. ty for ur answer

An identifier is a name.

local cool_variable

cool_variable is a valid identifier because it doesn’t contain any special characters, and doesn’t start with a number. Valid identifiers can only consist of A-Z, a-z, 0-9 and they can’t start with a number.

People likely do a["b"] instead of taking advantage of the syntactic sugar likely for the string colors.

a.b doesn’t highlight but a[“b”] does

I see i think i get it now thanks allot.
:))) greatly appreciate it

Using [] is useful to index objects that have names with a space in between. e.g [“Left Arm”].
Using a . will not allow you to index Left Arm as it is not a valid identifier (Uses a space in the name)

The . also helps with readability when looking through code. Dictionaries are referenced by [" "]. e.g

local MyDictionary = {Apple = “Fruit”, Orange = “Fruit”}
print (MyDictionary[“Apple”]) – prints Fruit

Most of this has been said already. I did not read the previous replies.

1 Like