Creating a saving and loading system for units

Hey there Dev Forum.
Here is what I want to achieve:

A system whereby I will be able to save my units (similar to all star tower defence, dungeon quest) along with their stats such as damage, levels, exp etc. I would also want to be able to load the units into the player’s inventory so that it will be able to be used.

The issue is:
I don’t know how. Despite reading several dev forum posts about saving these types of objects along with their attributes, I still do not understand how to do so. (note: i know the basics of data storing and loading data)

I’ve tried the following so far:
Very inefficient methods that I think would not be suitable for these types of data storing.

Lastly, I would like to say that I am not looking for full scripts. Rather, an explanation for dictionaries, saving and loading them. As I’ve heard that dictionaries are required to create a saving system like this.

I truly do not understand how they work. Thank you.

And you have heard correct. Dictionaries are essential for making save data. Dictionary consists of ‘key’ and ‘value’, you can access one value by using its key. About loading from or saving to player save. You just have to use dictionary to save value and carry it to different session that players may join. For loading, it happens when players first login into your game, so you have to use a method PlayerAdded to load all players information that they have progressed from the latest session to the current session. You can create that by using Instance.new to create an instance that can hold the value from player’s save.

local goldSave = Instance.new(“IntValue”)
goldSave.Name = “goldSave”
goldSave.Value = save[player.UserId][“gold”]
goldSave.Parent = player

This is easiest to generate what that player had when they join your game again.

For saving, simply change that goldSave from the server, and let the server changes the value inside that player’s dictionary (the save data) . ProfileService is probably what you want to use because of how simple it is.

Now, about what you want to achieve, saving units! You can create a dictionary with one key that holds all the units a player has. Like this…

local playerSave = {
    [171648382] = {
         {“unitsOwned”} = {
              [1] = {
                   [“unitName”] = “Alien”,
                   [“unitStat”] = {
                       [“unitLevel”] = 2,
                       [“unitDamage”] = 20
                   }
              }
         }
    }
}

And you can loop through these values and create what a player already owned.

1 Like

Oh I see!

However, how would I go about loading the units into the game? For example, accessing the dictionary and then having all the units load into the player’s inventory.

Do I create a folder with every unit with their stats first? And afterwards, how could I know whether the player has own that unit then clone the unit from the folder into the inventory?

Also, one more thing:
You wrote

[“unitName”] = “Alien”,
                   [“unitStat”] = {
                       [“unitLevel”] = 2,
                       [“unitDamage”] = 20

Would the stats in here be inside the unit model and can be changed?
Like:

["unitLevel"] = unit.Level.Value

etc etc

Thank you very much for your help!

Since the player save can only be written by the server (exploitable, though.) you can loop through the player save, get the name of the unit, and clone that unit from ServerStorage to player’s folder.

Second, to change the value inside, it can be done both ways.

From save to model

local modelDamage = player.UnitsFolder.Alien

modelDamage.Damage.Value = saveData[player.UserId][“unitStats][“unitDamage”]

From model to save


local modelDamage = player.UnitsFolder.Alien

saveData[player.UserId][“unitStats][“unitDamage”]
= modelDamage.Damage.Value

In case of static values, you dont have to create that value inside the save if you will be cloning it anyways directly from ServerStorage

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.