How would i create a dictionary using a script

I want to create a dictionary using a script, like how would I approach on how to doing this, I already know you can just create a dictionary manually using script such as this tutorial

--example--
local mydict = {
	["cats"] = {
		cat1 = "sam",
		cat2 = "mike",
		cat3 = "bark",
	},
	["guns"] = {
		["ak-47"] = {
			["name"] = "ak-47",
			["reload"] = 5,
			["type"] = "automatic"
		},
		["glock"] = {
			["name"] = "glock",
			["reload"] = {
				reload_speed = 2,
				animation_id = "rbxassetid://12346"
			},
			["type"] = "semi-automatic"
		},
	},
	["castles"] = {
		["bouncy_castle"] = {
			["protection"] = 2,
			["fun"] = 10,
			["coolness"] = 8
		},
		["regular_castle"] = {
			["protection"] = 8,
			["fun"] = 5,
			["coolness"] = 10
		},
	}
}

I don’t know how to really explain how, but how would I add indexes and values inside the indexes to the dictionary

like if I wanted to add another category such as sharks how would I do that using a script(not having it there already)?or getting rid a category such as castles? and what about about having to add values to the indexes like if i wanted to change the reload speed to one of the guns to a different number?

sorry to be very confusing and too many questions, but i have no idea what i’m doing

1 Like

You would do:

mydict.Sharks = {}
1 Like

oh i didn’t think about that, that looks simpler than i thought

1 Like

That could be achieved in the same way that the key-value pairs were created in the example code you provided. For instance, if you wanted to add another category / key called “dogs” that could be implemented like so:

local exampleDictionary = {
  ["exampleCategory1"] = {
        ["example1"] = "cool",
        ["example2"] = "nice"
    }

 -- Existing key value pairs would be here already
}

exampleDictionary["dogs"] = {}
-- This creates a brand new key called "dogs" with its value being an empty table

Is this referring to the tables that are associated with keys inside of a dictionary, such as in this example:

local exampleDictionary = {
  ["exampleCategory1"] = {
        ["example1"] = "cool",
        ["example2"] = "nice"
     -- Are you wanting to add more key-value pairs here?
    }

The value of the key can be set to nil (which means that it would no longer be holding a reference to any value):

local exampleDictionary = {
  ["castles"] = {
        ["example1"] = "cool",
        ["example2"] = "nice"
    }
}

print(exampleDictionary["castles"]) -- This would print the value associated with the key called "castles", which is the table that has "example1" = "cool" and "example2" = "nice"

exampleDictionary["castles"] = nil

print(exampleDictionary["castles"]) -- This would print nil since there is no longer a value associated with the key called "castles"

In this case, you can reference the existing key and set its value directly:

local exampleDictionary = {
  ["example1"] = {
        ["FavoriteFood"] = "Ice Cream",
    }
}

exampleDictionary["example1"]["FavoriteFood"] = "Macaroni and Cheese"

Resources

Tables Developer Hub Article

8 Likes

wow thank you, i spent so much time trying to find a solution and not much luck, but thank you for the help

1 Like