What are some practical applications of dictionaries?

I’m just curious about dictionaries because I’ve not really seen them used too often. I would like to know some of the ways they are used in game development on Roblox.

Do you mean dictionaries as in:

{["Cash"] = 100, ["Food"] = 5}

If so then they are extremely useful for organizing properties of a table. If I wanted to save something like the player’s cash and food and more, I could organize it by key and then later just call it with data.Cash or something. These are used all of the time, especially for the data thing. Basically whenever you have a table of data with different values representing different things, you will probably see a dictionary.


If this is not what you meant, I need some clarification.

They are used to store values with names, sort of like a real dictionary. Such as, apple, and then the value being 20. This could then be used for when you are making a fruit system and you want to know how many calories are in a single fruit. This way, instead of using up your entire script space in making a new variable for each fruit, you store it inside a single dictionary. Now, you could get items from the dictionary such as the apple, and then get the value of the item, such as 20. Hope this helps!

1 Like

Oh, so dictionaries can be used to store multiple values in data stores?

They’re used ALL the time.

For one thing you can use them to store data. For example, lets say I wanted to store data about all the different types of materials in my game.

local Materials = {
    Rock = {Value = 3, Rarity = 1},
    Wood = {Value = 5, Rarity = 3},
    Gold = {Value = 100, Rarity = 2000}
}

Now scripts can easily access this dictionary and get info on each material in my game.

Let’s say there’s a shop where you can sell materials. Well, when you sell a material the shop script can search for the name of the material you’re selling in this dictionary and get its value, and award you accordingly.

1 Like

I’ve used a dictionary for assigning keybinds.

1 Like

Yes, but I think you miss the point. The dictionaries themselves are references. You can also just use an array (or table in Lua) - but Dictionaries allow you to specify what you want to reference it by.

Instead of doing:

local data = {5,6} -- This is a table
print("The player has "..data[1].." lives left.")

You could do:

local data = {["Lives"] = 5, ["Cash"] = 6} -- This is a dictionary
print("The player has "..data.Lives.." lives left.")

It’s much more readable this way

5 Likes

Can I edit the value of one of the keys in a dictionary after it’s already been defined? For example if I have Rock = {Value = 3, Rarity = 1} as @ScriptingSausage uses, could I later change the value of rock in the dictionary?

You could do that with:

data["Rock"] = 100
1 Like

Yeah, you can edit the values in a dictionary. You can also have a dictionary representing every player in the game’s stats.

local Players = {
    ScriptingSausage = {Level = 2, TimePlayed = 532, Experience = 58}
}

then whenever someone joins, it adds their stats to this dictionary. Whenever a script wanted to check someone’s level for example they can get it from reading from this dictionary. This is just another example.