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.
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”.
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
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
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.
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.