Need help understanding two pairs of brackets next to a table (table[][])

I was loosely following a hotbar tutorial while trying to understand what they are doing.
this video by Shiroo
I don’t understand what is happening when they write

inputOrder[i]["tool"]

I’ve replicated it by making a dictionary and an array that orders the keys just like in the video.

local slots = {
	
	["SlotOne"] = "empty",
	["SlotTwo"] = "empty",
	["SlotThree"] = "empty",
	["SlotFour"] = "empty",
	["SlotFive"] = "empty",
	["SlotSix"] = "empty",
	["SlotSeven"] = "empty",
	["SlotEight"] = "empty"
	
}

local slotOrder = {
	slots["SlotOne"],slots["SlotTwo"],slots["SlotThree"],slots["SlotFour"],slots["SlotFive"],slots["SlotSix"],slots["SlotSeven"],slots["SlotEight"]
}

local function onAdded(added)
	
		for i=1, #slotOrder do
			
			print(slotOrder[i]["hi"])
		end
	print(slotOrder)
end

player.Backpack.ChildAdded:Connect(onAdded)

I understand that the “i” in “slotOrder[i][“hi”]” stands for whatever number the for loop is on. What I don’t understand is what the second brackets are doing. No matter what is put in there it will return nil.
I would like to know what the second bracket is for, which I haven’t found anything about on the documents. I’d also like to know how it is used in making a custom hotbar (Which I am trying to do). Thank you!

What you’re seeing is multi-dimensional indexing, which is born from multi-dimensional tables. Tables can contain tables, and to index the content within those contained tables, you must index them from the containing table:

local containing = {
    {"A", "B", "C"},
    {"D", "E", "F"},
}

print(containing[1][1]) --> A
print(containing[2][1]) --> D

The example makes containing a 2-dimensional table. I see no 2D tables in the code you provided, so it’s clear why a second index operation will not result in any value. You may have yet to complete the tutorial or made a mistake copying the code

1 Like

Of course it won’t output anything. Because you didn’t put any value in ["hi"]. I saw the video which you provided and the code was inputOrder[i]["tool"] = adding.

1 Like

just to note: this wouldn’t work in his example because “empty” isn’t a table.
slotOrder[i][“hi”] would be nil;
and if you try to add a value to “empty” (slotOrder[i][“hi”] = 123) you should get an error “attempt to index string with ‘hello’”

Roblox scripting tutorials are beyond unorganized. It’s highly likely the YouTuber intended for a different design while failing to adhere to it early on. It will be remedied at some point in the video

2 Likes

yeah i never follow any tutorials :man_shrugging:

i’d suggest op should just search for similar issues on DevForum and refer to the documentation

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.