Why is Datastore2 loading slowly?

Hello there, me and a team of developers have recently launched CodeClicker, however, the Datastore2 is having issues, such as slower loading. Players are complaining so much, that I have no choice than to come here to find a solution.
Current things that are in the saving list are:
Coins, EXP, Level, CPU Upgrades, SSD Upgrades, Rebirths, Levels, Maximum items, Pets table and equipped pets table.

Here is a part of the codes that includes saving:

local function saveeqdata(petsTBL,player) 
    --get data 
    local petData = datastore2(keyequipped, player) 
    --update datastore 
    petData:Set(petsTBL, defaultEquippedPets) 
    petData:Save(petsTBL, defaultEquippedPets) 
end 

game.Replicatedstorage.UnequipAll.OnServerInvoke = function(plr, mode)
    local petData = datastore2(keyequipped, plr) 
    local DATAFOLDER = 
     game.ServerStorage.DataFolder:WaitForChild(plr.UserId).Pets 
    local EQUIPDATA = DATAFOLDER.Parent.EquippedPets 

    local Table = {} 
    table.insert (Table, 1,"") 

    saveeqdata (Table,plr) 
    updateequippedpet (Table,plr) 
end

Here is the game:

I would appreciate if you find the solution.

You have not implemented DataStore2 correctly, you are failing to implement the “Datastore2.Combine” feature.

It looks like currently you handle all 10 datastore tables (Coins, EXP, Level etc) separately. This is throttling your game and will lead to data saving and loading issues.

Read into the (somewhat poorly) explained API made by @Kampfkarren here. I’m not going to lie, it could be explained in more depth, but with a little bit of trial and error you should be able to figure out the combine feature.

In short you will want to use the combine feature like so:

Datastore2.Combine(SOMEMASTERKEY, coins, exp, level, cpu, ssd, rebirths…) then ALL OF YOUR DATA WILL BE SAVED INTO ONE TABLE.

To get data you can then call
local someData = Datastore2(SOMEMASTERKEY, player):Get(getData())

Then you can access data by indexing it such as:

local coins = someData[“coins”]

This will give you the player’s coins.

It’s quite a tricky concept to explain in a brief forum post, but that’s where you’re going wrong.

2 Likes

actually, using the master key in DataStore2() calls is undefined behavior. You meant to get data from a specific “branch”.

Oh? I am fairly new to Datastore2 but have been using that method and have had no problems.

local playerData = ds2(mainKey, player):Get()
	local info = DataTable[item]
	if info[1] then
		playerData["Bools"][item] = true
		playerData["Ints"][info[3]] = playerData["Ints"][info[3]]-info[2] -- Minus their currency last.
	else
	playerData["Ints"]["Points"] = playerData["Ints"]["Points"] - info[2]
		if item == "PToH1" then
			playerData["Ints"]["Heads"] = playerData["Ints"]["Heads"] +1

This sort of code works fine for me, concluding all changes with “ds2(mainKey, player):Set(playerData)”.

How should I have structured code instead?

1 Like

You should be getting from a specific “branch”. For instance,

DataStore2.Combine("main", "coins", "gems")

local coins_data = DataStore2("coins", player)

Storing one huge table under the master key defeats the purpose of combined data stores.

Also, a.b is syntactic sugar for a["b"], so take advantage of it.

1 Like

Ah, right. Yeah I have been using just the single branch approach for most of my code, it was just at that particular point I wanted to change both “Ints” and “Bools” in one function, and so just found it easier to call all the data and change only those two branches, then setting the whole thing at the end instead of calling the two branches separately.

Although my approach is different, is it actually dangerous to do? If there’s no risk then I’ll probably keep doing it, and I don’t see why there would be any problems with my method. I appreciate that it is a bit weird, I don’t code much :rofl:.

And thanks, never knew that.

I believe UB is worth avoiding. The docs clearly state using the master key is undefined behaviour. Anything can happen.

1 Like

I will try this method and if it works great, I will mark it as Solution. Thanks :slight_smile:

1 Like

Please read incapaxx’s posts. This isn’t how you’re supposed to use Datastore2.Combine. This shouldn’t be the marked solution because it’s not guaranteed to work.

2 Likes

Do not do this. Using the master key outside of the combine statement is undefined, and will be broken in the future.

2 Likes