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


#82

Can you PM me on this? Realized might be spamming the thread a bit.


#83

In studio in play solo mode if I press the stop button it freezes for about 8 seconds due to the BindOnClose or something in the module when it saves in studio. Any way to stop this or do I just have to not make it save in studio using the variable?

image

(Keep in mind i am using the module when i took it from your inventory and took it like 1 month after the module came out so mine might be outdated)


#84

Right now you just have to make it not save in studio until the BindToClose studio patch is rolled out. Sorry for the inconvenience.


#85

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:


#86

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.


#87

Just insert into Inventory.Backpack:

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

Then Set again.

dataStore:Set(Inventory)

#88

Alright, thank you!


#89

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?


#90

Yes (sorry for the long response time).


#91

Alright, thank you!


#92

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.


#93

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.


#94

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


#95

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


#96

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


#97

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

#98

How are you doing this?


#99

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


#100

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)?


#101

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.