Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to add a value with key to a dictionary with the same key???

So what I mean is,

local dictionary = {
	["Thing1"] = {"yuh"},
	["Thing2"] = {"Harambe"}
	["Thing3"] = {"Trump cheeto"}
}

suppose I have that, what if i wanted to add another "Thing3" to the dictionary, resulting like this:

local dictionary = {
	["Thing1"] = {"yuh"},
	["Thing2"] = {"Harambe"}
	["Thing3"] = {"Trump cheeto"}
	["Thing3"] = {"Trump the orang"}
}

Is it possible to do this? Is there a way around this?

1 comment

0

accept my answer fat it's better

2 answers

✓ Accepted answer

No, you can't add two of the same key to a table. You could however make its value a table with the elements you want. Here's an example of that:

local dictionary = {
    ["Thing1"] = "yuh",
    ["Thing2"] = "Harambe"
    ["Thing3"] = {"Trump cheeto", "Trump the orang"}
}

1 comment

0

You know what, you gave me an idea, if I make 2 tales inside the single table for the key, I will successfully be able to make it look like i have 2 keys!!!

No. It is not.

Duplicate keys aren't possible. Lua will only use one of those key-value pairs. It wouldn't make sense for duplicates to be possible as there could be ambiguities.

Even if you try something like this:

local t = {a = 1}
t.a = 2

a field is overwritten, it now contains 2.

If you want, you can just add on to the {"Trump cheeto"} table.

table.insert(dictionary.Thing3, "Trump the orang")

This discussion is preserved as it was. New answers, comments, and votes are closed.