Needed Help with [] (Square Brackets) In depth explanation

Despite reading multiple sources about the [] and it’s functions it still confuses me to this day. I understand how [] are used to call out instances such as;

local Part = game.workspace["Part"]

and also understand how it can index categories in a table like;

local Table = {Blue,Green,Red,Purple,}
Table[2]
-- this will index Green

But when it comes to using brackets like this and I’ll give a good example most experience programmers would know;

local Players = game:GetService("Players")
local ProfileService = require(game:GetService("ReplicatedStorage").Modules.ProfileService)
local ProfileStore = ProfileService.GetProfileStore("Attributes_104123341313134552213",ProfileData)
local Profiles = {}

function OnPlayerAdded(Player)
	local ProfileSession = ProfileStore:LoadProfileAsync(tostring(Player.UserId), "ForceLoad")
	if ProfileSession then
		ProfileSession:AddUserId(Player.UserId)
		ProfileSession:ListenToRelease(function()
			Profiles[Player] = nil -- THE Profiles[Player] 
			Player:Kick()
		end)
end

Now don’t mind the profile service, I do not need help with that as I already fully understand it. The thing I need help with in what the [] in

Profiles[Player]

mean in code it general. I view it as something it can’t understand as it’s not a string so it can’t be indexing something in the workspace or game in general and it also isn’t a number to call out the order of the table and this is how I view it which I assume to be wrong. So my main question is in codes like this, what does the [] index or do, and what and when should I use it.

1 Like

The way it works in

local Part = game.workspace["Part"]

Is the same as doing

local Part = game.workspace.Part

They both have their uses, but you may need to use the bracket notation if

  • The Instance you’re trying to get starts with a number
workspace.1Part -- Will not work
workspace["1Part"] -- Will work
  • The Instance you’re trying to get has a space in the name
workspace.The Part -- Will not work
workspace["The Part"] -- Will work
  • If you’re trying to get an Instance by the value of a variable/a string
local var1 = "Part"

workspace.var1 -- Will not work, instead it will try to get a part named var1
workspace[var1] -- Will work as it will try to get something called Part

As for

Profiles[Player] = nil

It’s setting a dictionary key to nil (Not an array because by the code, Player is a player instance)

A dictionary is a table that uses a key value pair

local dict = {
    Hello = "Hi",
    SandwichType = "Peanut butter"
}

dict["Who"] = "Bob" -- defining mid script

print(dict["SandwichType"]) -- Peanut butter
print(dict["Who"]) -- Bob
print(dict["Pen"]) -- nil because not in dictionary

Setting a key to nil makes the key no longer exists, useful if that key is no longer needed

2 Likes

Another example is we have a tool but the tools name has two words in it.

Would not work:

Player.Backpack.Wood Sword

Would work:

Player.Backpack["Wood Sword"]
2 Likes

What you need to understand is that indexes (or indices if you’re into math) don’t need to be strings. In your second block of code, Table[2] gets the value located at the index 2.
Profiles[Player] gets the value located at the index Player. Indexes can be whatever you want, even if they don’t make a whole lot of sense.

local MessyExampleTable = {
    game.Players.Fraudsstar = {Gold = 100, Health = 75, Speed = 24},
    game.Workspace.Baseplate = 1300,
    game.Workspace.Baseplate.BrickColor = "ExampleValue",
}
print(MessyExampleTable[game.Workspace.Baseplate.BrickColor])
--> ExampleValue

We aren’t setting the BrickColor of the Baseplate though, we are creating a new element in a table with the key/index/name being that BrickColor. To access it, we need to use square brackets because it has a weird key/index/name. We can’t do this, obviously

print(MessyExampleTable.game.Workspace.Baseplate.BrickColor)

The . operator only works on string names which start with a letter and contain no weird symbols.

3 Likes

So the dictionary Dict["Who"] = "Bob" would create itself when the script is fired within the table like an Instance.new() but for tables ?

A key doesn’t have to be a string that is why it works. it can be anything.
edit:
Also its not indexing an object in the workspace. Most likely its indexing the objects reference, which can be stringified. This has more to do with how roblox works in the background

1 Like

Square brackets are used to create or modify an index in an array or map (also known as a dictionary). A dictionary is similar to an array but instead of being in a specific order dictionaries have no order and are instead indexed via a key (a key can be almost any datatype, but it is usually a string).

2 Likes

If you already have the dictionary made, what it does is create a new key value pair, it’s the way you insert something into a dictionary, similar to how table.insert() adds a new element in a table array,

Arrays are technically dictionaries, the only difference between them is that arrays use numbered keys

Example

local tbl = {5, 6, "Hi"}

print(tbl)

-- Result

{
    [1] = 5,
    [2] = 6,
    [3] = "Hi"
}

whereas a dictionary uses anything you desire as others have already mentioned

1 Like

Oh yeah this makes sense. So the [] are used to index things within the table and it doesn’t have to be a string which means that it has pretty much the same function as the ones used without indexing a table.