Any better way of saving large amounts of Data

I have been scripting for a few years now, and have constantly used DataStores, however I am unsure as to if there is a better way of storing data. Most of the data I need to store is names of items players have purchased, like vehicles, firearms, etc.

So far I have been doing this all in lists, using arrays to store multiple values to 1 data store, however I have recently needed to store more data, that all falls under the same category, i.e. vehicle colour, vehicle upgrades, etc, and I am wondering if there is a better way to do it rather than using multiple data stores.

Here is an example of the many data stores I use for different data:

local CashDataStore = game:GetService("DataStoreService"):GetDataStore("SharpShooter2CashDataStore632")
local PlayerMetadata = game:GetService("DataStoreService"):GetDataStore("SharpShooter2PlayerMetadataStore633")
local WeaponsData = game:GetService("DataStoreService"):GetDataStore("ShapShooterWeaponsPurchases634")
local Cosmetics_Skins = game:GetService("DataStoreService"):GetDataStore("SharpShooterCosmeticsSkins635")
local Cosmetics_Weapons = game:GetService("DataStoreService"):GetDataStore("SharpShooterCosmeticsWeapons635")

I feel like there is a better and more efficient way of doing this, hence why I am asking for second opinions.

1 Like

Why don’t you do something like mysql? Mysql has a database with tables in it. So you could just use 1 data store and its value is something like:

  • Players

    • Player1
    • Player2
  • Weapons

    • Ak

You can even try to have more in-depth detail on the players, in mysql I use a JSON value which allows me to give even more info:

  • Players
    • Player1
      • Weapons
    • Player2

Of course, for you all these values are tables.

And if you are worried thinking that editing the entire data store is a bad idea then don’t worry. Using :UpdateAsync you can ensure that the data you are setting is up-to-date using the variable they give you. You will never set 2 at a time since :UpdateAsync waits for the current request to be set. (You can not yield)

1 Like

Thanks for the idea!

How exactly would I do this in Roblox. I know enough about MySQL to know what it is but nothing more, and have never really played around with it. The main concern I have with datastores is mostly the fact I am trying to set a large amount of data when a player is leaving. It’s also using up quite a few datastores, and with the increasing amount of Data that I am needing to save I just keep adding more datastores.

Don’t worry about the size of the server. The max size is 4MB, I do not know where this is stored but both the RAM and disk size of the servers are way bigger than 4MB. So even if you wanted to you couldn’t crash because of memory.

In relation to :UpdateAsync you use it like this:

:UpdateAsync(**key here**, function(key, data)
    --Note: key is the key you are trying to set, in other words:
    --key == **key here**

    return data; --You are returning what will be set in the database
end);

Real example:

:UpdateAsync("Players", function(key, data)
    --Note: key is the key you are trying to set, in other words:
    --key == 'Players'

    data[366671978].Weapons.Ak47.Colour = "#ffffff";

    return data; --You are returning what will be set in the database
end);

This way you only edit 1 index and ensure that everything else remains the same. Note: Color3 values can’t be stored in databases so that is why I made it a hex string for this example.

I am still slightly lost. How exactly do I use this to store multiple values in 1 datastore?

This is simple. You just need to make nested arrays, like:

local MyData = {
  "PlayerId" = 12312345,
  "Weapons" = { -- Our nested array;
    1 = {
       Color = Enum.BrickColor.Red
     }
  },
  "Backpack" = {} -- The same thing again;
}
1 Like

Everything is a table.

First is the data store: game:GetService("DataStoreService"):GetDataStore("Main");
Every key in the data store will be a table. This means you can do:

local myDataStore = game:GetService("DataStoreService"):GetDataStore("Main");

local allPlayers = myDataStore:GetAsync("Players");

Since everything is a table you can have the “Players” key to have all players:

  • Datastore
    • Players
      • Player1
        • Weapons
      • Player2
        • Weapons

Lets say you are getting the weapons data from a player, you can do this:

local myDataStore = game:GetService("DataStoreService"):GetDataStore("Main");

local thePlayer = myDataStore:GetAsync("Players")[366671978];
print(thePlayer.Weapons);

Edit: Just like @WizardPy said.

3 Likes

I think that would be better if he could try to use it like NoSQL instead of SQL.

Since SQL is based as Structural Query Language, in DataStore you cannot structure your data as you want. You can put everything in there, but not as you want to be.

NoSQL is the right approach for that because DataStore is to be flexible and easy to learn, except of structural or more things.

1 Like