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

Gotcha, thank you very much. Ill tell you when we publish our game!

3 Likes

Awesome stuff, though I have one quick question.

How would I save data to a player if they aren’t present in the server?
To elaborate I noticed I must pass a player object through to get the player’s data, instead of a traditional UserId int.

DATA_STORE(DATA_KEY,plr):Get(NewPlrDataTable)
-- What I'd like to do
DATA_STORE(DATA_KEY,UserId):Get(NewPlrDataTable)

To reasses, I need to grab and save data to a player that is not in a server. Is there a safe way of doing this with DataStore2?

I do apologize if there’s an obvious answer I missed. Thanks again, ingenious stuff my guy. :grin:

Edit: After dabbling a bit I realized that you can only use a player instance. Was hoping to use this method for a way to ban during run-time on players that are not present in the server using the same Data-Store, but I believe I will have to use alternatives.

3 Likes

DataStore2 doesn’t support this innately because it’s not completely safe, users can be in different servers where the cache won’t recognize the new data and whatnot. DataStore2’s code isn’t very complex, so you can figure out how it works and emulate it.

5 Likes

A wrapper would just use a separate ordered data store and set the values whenever DataStore2 updates.

Could you possibly go more in depth on this?

1 Like

As in, you’d have to manually make an ordered data store and whenever you save in DataStore2, you’d update that ordered data store too.

2 Likes

Thank you for making the video tutorial! This is great with the addition of the text tutorial in this post.

1 Like

Alright I am new to DataStore2 and would like to use it in my game, but I’ve been trying to Set/Get a table but it doesnt seem to work. Any Ideas anyone?

2 Likes

You can just use :Get and :Set normally with tables, they’re nothing different. What is giving you trouble?

2 Likes

Yeah, I figured that’s what you meant but I wanted to confirm.

Wouldn’t saving another DataStore every time DS2 updates cause too many requests?

2 Likes

Not whenever it updates, but when it saves (with :AfterSave).

3 Likes

Totally overlooked :AfterSave, thanks! :slight_smile: Hopefully that was the last question!

4 Likes

i’m probably dumb or something, but how would i look through the data of a player in Datastore Editor (by Crazyman32) with this system?

2 Likes

@Kampfkarren I am having trouble with this. I’m sort of familiar with DataStores, i can do them, however, i am trying to save a lot of values in a table. But i’m not familiar with serializing and deserializing, i’m not sure what they mean either.

I cant find any help anywhere with this. Lets say you have a table with a lot of values stored inside, and its stored into the one data store, how do you go about picking that one value and changing it in the store itself. I understand very well if it was just one value in the store, but i cant seem to wrap my head around saving tables, and many many values. Please help!

As an example i used the basic serialized and deserialized example you gave, and am failing at integrating it… because i dont know how in this case :confused:

local SpellDefaultTab = {
["Basic Shield"] = true;
["Orlora"] = true;
["Branda Mirun"] = false;
["Valroc Mirun"] = false;
["Varia"] = false;
["Orlovium"] = false;
["Caecus"] = false;
["Crimbiu"] = false;
["Crimbastu Lackra"] = false;
["Fustama Truvista Livistu"] = false;
["Orlovick"] = false;
["Flithrumar Calivum Brathcavan"] = false;
["Duble Baldadre Hadra"] = false;
["Ible Siezumeum"] = false;
["Blonia Prostagalera"] = false;
["Portalium"] = false;
["Arathalat"] = true; 
["Basic Shield Mastery"] = 2;
["Orlora Mastery"] = 2;
["Branda Mirun Mastery"] = 2;
["Valroc Mirun Mastery"] = 2;
["Varia Mastery"] = 2;
["Orlovium Mastery"] = 2;
["Caecus Mastery"] = 2;
["Crimbiu Mastery"] = 2;
["Crimbastu Lackra Mastery"] = 2;
["Fustama Truvista Livistu Mastery"] = 2;
["Orlovick Mastery"] = 2;
["Flithrumar Calivum Brathcavan Mastery"] = 2;
["Duble Baldadre Hadra Mastery"] = 2;
["Ible Siezumeum Mastery"] = 2;
["Blonia Prostagalera Mastery"] = 2;
["Portalium Mastery"] = 2;
["Arathalat Mastery"] = 2;
}




Players.PlayerAdded:Connect(function(Player)
local SpellsStore = DataStore2("SpellsStorage", Player)
local SpellData = Instance.new("Folder")
SpellData.Name = "SpellData"
SpellData.Parent = Player

for statname, statvalue in pairs(SpellDefaultTab) do
	if type(statvalue) == 'number' then
		local intvalue = Instance.new("NumberValue")
		intvalue.Name = statname
		intvalue.Parent = SpellData
	elseif type(statvalue) == 'boolean' then
		local boolvalue = Instance.new("BoolValue")
		boolvalue.Name = statname
	
		boolvalue.Parent =  SpellData
	elseif type(statvalue) == 'string' then
		local stringvalue = Instance.new("StringValue")
		stringvalue.Name = statname
		
		stringvalue.Parent = SpellData
	end
end

SpellsStore:BeforeInitialGet(function(Spells)
	local deserialized = {}
	
	for _,SpellId in pairs(Spells) do

		for key,value in pairs(SpellDefaultTab) do
			if value == SpellId then 
				table.insert(deserialized, key) 
			end
		end
	end

	return deserialized
end)

SpellsStore:BeforeSave(function(Spells)
	local serialized = {}

	for _,SpellVal in pairs(Spells) do
		table.insert(serialized, SpellDefaultTab[SpellVal])
	end

	return serialized
end)

SpellsStore:Get({})

end)

3 Likes

How do i set a value that is inside a table thats stored? Something like this?

	local SpellsStore = DataStore2("SpellsStorage", Player)
for key,value in pairs(SpellsStore:Get({})) do
	if key == "Orlovium" then
		SpellsStore:Set(true)
	end
end
2 Likes

There’s nothing special about tables for DataStore2. How would you set the table normally? Just do that then :Set with the new table.

2 Likes

Make sure this works without [de]serialization, if it does then that means it’s an issue with your BeforeInitialGet/BeforeSave implementations.

2 Likes

It’s a hassle, but it’s possible. From what I remember, this is how you do it.

  1. Figure out what the data store name is going to be.

A DataStore2 name consists of two parts: KEY/USER_ID. The key is either the master key if you’re using combined data stores, the normal key otherwise. Let’s say our combined data store key is DATA and we’re looking for the data of ROBLOX. We would search: DATA/1.

  1. Toggle OrderedDataStores and Query.
  2. Write down the value of the most recent result.
  3. Turn off OrderedDataStores, and search with the key being what you just wrote down.
22 Likes

Thankyou @Kampfkarren, i will give it a try :slight_smile:

1 Like

oh wait real quick, any specific way of incrementing a value in the table within the datastore?

1 Like

No, don’t think of tables as anything different in terms of DataStore2.

2 Likes