I have a dictionary with some values:
local Dictionary = {
["Key1"] = "Roblox"
["Key2"] = game.Players.LocalPlayer
}
But i don't know how to add another value with key to it.
I have a dictionary with some values:
local Dictionary = {
["Key1"] = "Roblox"
["Key2"] = game.Players.LocalPlayer
}
But i don't know how to add another value with key to it.
You can insert new keys in a dictionary (table) by getting, and setting their index (as you would a variable). Take this for example:
local Dictionary = {
["Key 1"] = 1,
["Key 2"] = 2,
}
-- For obvious reasons, prints nil.
print(Dictionary["Key 3"])
-- However, we're able to set it with a new index like this.
Dictionary["Key 3"] = 3
-- We get "3".
print(Dictionary["Key 3"])
You can also do this using "." to index the table, if the key follows the same "rules" as any other variable (i.e, no spaces, doesn't start with a number, etc). Like this:
local Dictionary = {
Key1 = 1,
Key2 = 2,
}
Dictionary.Key 3 = 3 -- This will error, so we can use brackets instead [""]
Dictionary["Key 3"] = 3
Dictionary["Key4"] = 4
Dictionary.Key5 = 5 -- Works just as fine as ["Key5"] = 5
print(Dictionary["Key 3"])
print(Dictionary.Key4)
Hope this helped, let me know if you have any questions.
This discussion is preserved as it was. New answers, comments, and votes are closed.
3 comments
You can't use
dic.["key"]. Brackets or dots, not both.I know, that's why i used brackes to get keys with a space in them, and vise-versa with a "." . That Dictionary.["Key 3"] one was a typo.
how would i define the key's position?