How to use DataStore2 - Data Store caching and data loss prevention

I’ve had a ton of people reporting data losses in the past so I decided to switch to DataStore2 on my game. It was really easy to set up (literally easier than normal datastores) and I’ve had absolutely no incidents since. Thank you! :grinning:

21 Likes

Got a few questions because I’m dumb and can never understand anything first time.

Say I have a table like so that I keep the players equipped items and general items in.

local Default = {
	Equipped = {
		Shovel = "Starter",
		Backpack = "Starter"
	}
	Shovels = {"Starter"},
	Backpack = {"Starter"},
}

So I would do

local Inventory = DataStore2:Get(Default)

Which means if there’s no inventory, it’ll return Default / Starter Inventory

How would I then go about say, adding a new backpack to the list of backpacks that the player currently has? I read through the post a few times but couldn’t figure it out so really sorry if it’s obvious.

8 Likes

Just insert into Inventory.Backpack:

table.insert(Inventory.Backpack, "NewBackpack")

Then Set again.

dataStore:Set(Inventory)
9 Likes

Alright, thank you!

6 Likes

Another quick question about InventoryData:Get(Default)

That will return the Default table if there’s nothing saved, but does it also then set / save the players InventoryData to the default table?

5 Likes

Yes (sorry for the long response time).

6 Likes

Alright, thank you!

6 Likes

Another quick question. After using :Set(), when I test in Studio and stop the session. It stops responding for 5-10 seconds and then stops and gives me the error: Not running script because past shutdown deadline (x260)

Is this something to do with me? Or just how the script works? I have SaveInStudio set to true in ServerStorage and studio can access API.

6 Likes

That’s an issue with BindToClose and it’s caused because you enabled saving in Studio. You’ll either have to deal with it, or turn it off and wait for Roblox to fix the issue.

6 Likes

Okay, so right now we can’t test in Studio? I can live with that haha.

4 Likes

You can, just turn off SaveInStudio. You won’t be able to save it when you’re testing.

5 Likes

Ah sorry, I meant test saving in studio. Thanks for the help though!

5 Likes

Another question because I’m still dumb haha.

EmptyBackpack.OnServerEvent:Connect(function(Player)
	local Data = DataStore2("Inventory", Player)
	local Inventory = Data:Get()
	Inventory.Snow = 0
	
	Player.Character.Backpack.Storage.SurfaceGui.Amount.Text = "0/"..Inventory.MaxSnow
--	Inventory.Gold = math.floor(Inventory.Snow*GoldMultiplier)
	Data:Set(Inventory)
	print(Data:Get().Snow)
end)

This is inside the main sever script. The print; prints 0 like it should. But then when I add more Snow to the backpack in the tool script, it hasn’t changed back to 0. Not sure what i’ve done wrong (probably using :Set() wrong)

This is the code in the tool (though it’s in a module script)

function Shovel.Dig(Backpack, Pile, Data, Damage)
	local Inventory = Data:Get()
	
	local Snow = Inventory.Snow
	local MaxSnow = Inventory.MaxSnow
	
	local Current = Pile.Current
	local Max = Pile.Max.Value
	
	if Snow + Damage > MaxSnow then
		Inventory.Snow = MaxSnow else
		Inventory.Snow = Snow + Damage
	end
	Backpack.Text = Inventory.Snow.."/"..Inventory.MaxSnow
	Current.Value = Current.Value - Damage
	if Current.Value <= 0 then Pile:Destroy() end
	print(Inventory.Snow)
	Data:Set(Inventory)
end
10 Likes

How are you doing this?

8 Likes

Sorry, I wrote the wrong thing. I updated my comment with the correct and additional details.

5 Likes

Your code all looks fine to me, which is concerning. Have you tried isolating the issue (e.g. putting prints to make sure it’s actually being read as the old value and not your code setting it back somehow)?

7 Likes

Yeah, just did a ton of prints. Prints 0 in the emptybackpack function after I do the arithmetic and set it. But then when I go to dig and add stuff to my backpack, it’s the original value.

The only places I used the datastore module is in the two blocks of code I provided (apart from in CharacterAdded but that’s only to do with equipping the backpack / shovel). I probably did something wrong but idk what.

7 Likes

Try replacing local Inventory = Data:Get() with local Inventory = DataStore2("Inventory", Player):Get(), perhaps there is an issue with caching/differing keys?

7 Likes

In any particular block or both?

5 Likes

Second one.

6 Likes