Are there any differences between using ["Name"] or just Name for the key of a table?

Code for copy paste
local ToolBarKeybindsTable = {
	["Select"] = "X",
	["Move"] = "C",
	["Rotate"] = "R",
	["Resize"] = "G",
	["Part"] = "V",
	["Joint"] = "One",
	["Constraint"] = "Two",
	["SolidModeling"] = "Three",
	["PaintBucketTool"] = "Four",
	["Delete"] = "Five"
}

local ToolBarKeybindsTable = {
	Select = "X",
	Move = "C",
	Rotate = "R",
	Resize = "G",
	Part = "V",
	Joint = "One",
	Constraint = "Two",
	SolidModeling = "Three",
	PaintBucketTool = "Four",
	Delete = "Five"
}

Also, should I call them “Table” or “Dictionary”?
Thanks!

1 Like

No there’s no difference.

In the variable name? table.
I mostly see people (on the forum) calling it a Dictionary so the reader knows what they are talking about without seeing the code.

You can also call it “Settings”, if the player can change the Keybinds.

Table is more general than Dictionary, both are fine to use, but dictionary is more specific
Table includes both array and dictionary

1 Like

Both are tables one is using explicit keys the other is not.

Are you absolutely positively sure that there’s no difference? Like 110%? I’m nervous something down the line might break if I use the bottom method :stuck_out_tongue:

What does this mean? :o

Not 110% sure but 98%
You can print and experiment with both to see if there’s a difference.

I think LuaU takes one of those and convert it to the other one (if their index is a string), so technically they’re the same.

These are both dictionaries and functionally the same. The only benefit of putting it in square brackets is that it allows you to set the key to pretty much anything except nil, booleans and floats, whereas without you are limited to 1 word strings.

However you can’t compare the two tables by using == or ~= because their memory locations are different and are seen as different objects. Instead you will need to compare each element in the dictionary one by one. Hope this helps!

to be 100% sure we need to see deasembler of code but im pretty sure in --!optimize 2 there is 0 differance
Using [“”] is required if you are using specific characters that otherwise would not work aswell if you are making a hash table like:

local ins = Instance.new("Part")

local hashTable:{[Instance]:string} = {
[ins] = "Message"
}

I checked this way:

for i, v in someTable do -- someTable is the top one, and otherTable is the bottom one
	if otherTable[i] == v then print("true") end
end

It printed true for all the elements (10 times) so I guess it works :D

Edit: I now realize it should be k, v not i, v :p

1 Like

Okay, so what I’d want to put goes inside the brackets, if I want a string, I put it in quotes, if I want a part, I don’t. Awesome :D

No there is no difference other than you can use just about any character when u use a string enclosed in square brackets as opposed to a single word that can’t start with a non-alphabet character.

Yes, thank you. I have a variable coming up that starts with a number (for alphabetical organization reasons) so this will be useful.

Thanks!

Did I do the typechecking correctly? I wanna get into the habit of using it more often :D

Code
local ToolBarKeybindsTable:{string:string} = {
	["Select"] = "X",
	["Move"] = "C",
	["Rotate"] = "R",
	["Resize"] = "G",
	["Part"] = "V",
	["Joint"] = "One",
	["Constraint"] = "Two",
	["SolidModeling"] = "Three",
	["PaintBucketTool"] = "Four",
	["Delete"] = "Five",
	["DeselectAllButtons"] = "Space"
}

local PartKeybindsTable:{string:string} = {
	["1"] = "One",
	["2"] = "Two",
	["3"] = "Three",
	["4"] = "Four",
	["5"] = "Five"
}

local JointKeybindsTable:{string:string} = {
	["1"] = "One",
	["2"] = "Two",
	["3"] = "Three",
	["4"] = "Four",
	["5"] = "Five"
}

local ConstraintKeybindsTable:{string:string} = {
	["1"] = "One",
	["2"] = "Two",
	["3"] = "Three",
	["4"] = "Four",
	["5"] = "Five"
}

local SolidModelingKeybindsTable:{string:string} = {
	["1"] = "One",
	["2"] = "Two",
	["3"] = "Three",
	["4"] = "Four",
}

Thanks!

If you have such question on future best guess is setting --!strict mode
Also not really correct becouse it should’ve been: [string]:string instead
{string} is equal to {[number]:string} btw

for the tables i think you should do

local WhateverKeybindsTable: {[string]: string} = {}

not quite, instead you would do

local ToolBarKeybindsTable : {[string] : string} = {
["Item"] = "MyString",
}

but you are close!

Thank you guys!

You call one by it’s a key or by it’s place like print(ToolBarKeybindsTable[“Joint”]).
The other you call by it’s place only like print(ToolBarKeybindsTable[6]).

Both in this case would print the string “one”.