What are square brackets used for?

Since I’m learning about indexing, I might as well learn what are square brackets and when to use them.

2 Likes

The most common practice of using these brackets are for indexing. Either accessing through instances or, more commonly, tables. Any value inside the brackets represent the index value.

However, instances are usually using their own methods or direct accesses via addressing the name of instances.

7 Likes

so will game.Players[“LocalPlayer”] work the same as game.Players.LocalPlayer because im indexing the local player

7 Likes

They are essentially working the same. If I reckon it correctly, someone had mentioned that there is a minimal difference between that I do not know of as of now.

Apparently, it’s better to use the square brackets when the name contains whitespaces.

1 Like

Last question periods in lua is indexing also? because in game.Players im getting the players from the current game?

1 Like

It is the syntax after all. That is correct, but sometimes you don’t use the indexing only and should the instance be replicated later, alternate methods substitutes over it such as game:GetService().

8 Likes