Having trouble inserting Key + Value into dictionary

So effectively I’m trying to insert a table into another table but I can’t seem to do it, an example of the outcome I’m trying to achieve would be.

Dictionary = {
[PLAYERNAME] = {
[KEY1] = {Duration = 0, Value = false},
[KEY2] = {Duration = 0, Value = false}
},
[PLAYERNAME] = {
[KEY1] = {Duration = 0, Value = false},
[KEY2] = {Duration = 0, Value = false}
}

i want to be able to remove and add ’ Key1 ’ or ’ Key2 ’ from their respective player table using code but keep getting errors / new variable over writing the entire thing.

I’ve tried to do the following

Table[Player.UserId][Key] = Duration

but this results in an error saying Key doesn’t exist, yet I’m trying to create said Key w/ Duration and whatever other variables under Player.UserId without interrupting / changing or removing any other Keys / Variables under Player.UserId.

Apologies if this is worded weirdly!

I should mention that isn’t the only solution I’ve tried and I’ve been unable to find any resources online that are able to help with my issue. My code is as follows

( Module )

function DebounceHandler:Create(Table, Player, Key, Duration)
	if Player and Table and Key and Duration then
		Table[Player.UserId][Key] = Duration
end

( Main Script )

DebounceHandler:Create(DebounceSystem, game.Players.F_emBoyz, "Magic", 60)
DebounceHandler:Create(DebounceSystem, game.Players.F_emBoyz, "newKey", 242)

so i think the problem is you need to create each layer one at a time if that makes sense like instead of having an empty dictionary like

dict = {}

and then running

Table[Player.UserId][Key] = Duration

you need to do

Table[Player.UserId] = {}
and then 
Table[Player.UserId][Key] = Duration

Make sure you’re using the correct operator. Brackets operator [] are for using a variable/a different value as the key. Dot operator . is when you index for exactly what you wrote.

Table[Player.UserId][Key]
--is not the same as
Table[Player.UserId].Key

Yea so I’ve tried something that I believe is similar

function DebounceHandler:Create(Table, Player, Key, Duration)
	if Player and Table and Key and Duration then
		if Table[Player.UserId] then
			Table[Player.UserId]["Stats"] = {Key = Duration}
		else
			Table[Player.UserId] = {["Stats"] = {}}
			Table[Player.UserId]["Stats"] = {Key = Duration}
			print("Table Created! : did not exist")
		end
	end
end

read what I just said. Key is the name of the key, but by using brackets you’re trying to use the variable called Key, which doesn’t exist

So if I understand correctly you mean use . as if I’m indexing for exactly what I wrote? Because I’m trying to use a variable ( Key ) which could be ANY string but I’l try this out now

Here’s an example. All of these are the same:

table.a
--
table['a']
--
local var = 'a'
table[var]

image
it works for me^^
output:

1 Like

When you create a dictionary table, you need to set the value to a table first before you can start indexing a key for that value. Example below.

local Item = {}
Item["ItemName"] = {}
Item["ItemName"]["ItemValue"] = 10

2 Likes

This makes a lot of sense, though I think I tried something similar but must’ve still continued to do my original method in another part of my code. I’ve fixed the issue now. Thanks to everyone who responded quickly!

1 Like

bro are you fr she literally wrote out the exact same post as me with different words

5 Likes