DataStore2 Optimization Question

In my game i am using DataStore2 to save data
I have implemented table saving for customization and quests, and everything works perfectly fine. Those wont affect performance much, because players customize their character only once, and quests store is updated rarely (only when getting a new one or completing it)

However i am also working on an inventory table datastore. It works without any issues for now, but i am curious if this will be too heavy for performance, because in certain situations inventory store may get updated every second for each player.

If this will be heavy on performance, how can i improve this?

My code chunks

local DataStore2 = require(ServerScriptService.DataStore2)
local defCust = {Gender = "Male", Face = "86487700", Hair = "0", Color = "248,248,248", Shirt = "http://www.roblox.com/asset/?id=5583364865", Pants = "http://www.roblox.com/asset/?id=1838299111"}
local defQuest = {Quest1 = 0, Quest2 = 0, Quest3 = 0, Quest4 = 0, Quest5 = 0, Quest6 = 0, Quest7 = 0}
local defInventory = {Pickaxe = 1, Golddust = 0}


DataStore2.Combine("DATA", "xp", "nextxp", "lvl", "souls", "gold",
	"strength", "toughness", "agility", "wisdom",
	"charcustom", "quest", "inventory",
	"checkpoint"

) --, "maskid", "armorid", "weaponid", "organization", "checkpoint")

And this is an example of inventory store getting updated frequently

if hit.Name == "GoldMine" and state.Value == "Attacking" then
		
		inventTable["Golddust"] += 1
		inventStore:Set(inventTable)

	end

Any ideas how can i improve this?

Still looking for solutions :slight_smile:

I don’t know but you can directly simplify the “defCust” part a bit

local defCust = {
	"Male", -- Gender
	"86487700", -- Face
	"0", -- Hair
	"248,248,248", -- Color
	"rbxassetid://5583364865", -- Shirt
	"rbxassetid://1838299111" -- Pants 
}

Upping this one more time because this is a big part of the game. Still looking for solutions

You shouldn’t be updating the datastore every time a player does something with their inventory, maybe save it every few minutes and when the player leaves the game.

Ok so save on Player.PlayerLeft, and on playeradded connect autosaving once a minute or so. How would i go about accidental disconnecting (player loses inet or computer shuts down, or server shuts down). Are there any events for that?

PlayerRemoving (not PlayerLeft, which isn’t an event) will fire whenever the player disconnects, even if they’ve been kicked, and if the server shuts down you can just do

game:BindToClose(function()
    print("Server is shutting down")
    --Save data
end)

Which I assume Datastore2 does automatically.

in DataStore2 we dont use BindToClose. We just do store:Set(value), or store:Increment(value). All BindToCloses are done automatically.

So maybe those store:Set() things wont affect performance?

DataStore:Set

Set(newValue: any)

Will set the cached value in the data store to newValue . Does not make any data store calls, and so will never yield.

I found this in DataStore2 API page. It says that set doesnt make any datastore calls? What do you think about this?

Ok so, by the looks of it you don’t need to do any of what I just said, apparently Datastore2 automatically saves data to Roblox’s datastores when the player leaves…? I’m honestly not very well experienced with it. Maybe you can ask your questions on @Kampfkarren’s post?

1 Like