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

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

ok in whichcase how would i increment a specific value in the datastore? just Datastore:Increment(1) how does that find the value in the store?

1 Like

:Increment only works if the data value is a number. That’s why as said before:

3 Likes

i understand that it needs to be a number… so what i am to understand is that if the datastore is a table then specific values inside that might be a number cant be incremented with that function. Increment the value outside of it,and reimplement the new table in the store.

2 Likes

Yes. How would you increment the table’s value normally? Whatever it is, do that, then call :Set on the table–DataStore2 doesn’t care how you set the variable.

2 Likes

I dont need to put anything in the parentheses? or do i put the new table in the Datastore:Set(NewTable)

1 Like

Again, just set it normally. Tables aren’t anything special.

2 Likes

I’m sorry i feel so dumb not totally understanding how the datastore indexes each name with the value attached into the 1 store. I feel like such a pest rn XD.

2 Likes

Again, don’t think of tables as anything different. When you call :Get(), it’ll return the table. Do whatever you want with the table. Then call :Set. I’m not sure what else to say.

local things = thingStore:Get()
things.Something = things.Something + 1
thingStore:Set(things)
4 Likes

that is very helpful thankyou :slight_smile:

again i apologize for sounding like a total doofus XD, i guess we all have things we just cant wrap our heads around. You are very patient thank you.

4 Likes

I’m sorry just one more thing: This is what im doing to get the values into the datastore to set the values from the default table, into the datastore…

local SpellsInStore = SpellsStore:Get()
local function UpdateSpellStore(UpdateVal)
	for statname,statval in pairs(SpellDefaultTab) do

		SpellData[statname].Value = SpellsStore:Get(statname.UpdateVal)
		
	end
end
for i,v in pairs(SpellDefaultTab) do
	UpdateSpellStore(v)
end

SpellsStore:OnUpdate(UpdateSpellStore)

end)

basically i want the statname to equal the updateval in the datastore.

2 Likes

Ahh it should be GetTable:({statname = UpdateVal})

    local function UpdateSpellStore(UpdateVal)
	for statname,statval in pairs(SpellDefaultTab) do
		SpellsStore:GetTable({statname = UpdateVal})
		local SpellsInStore = SpellsStore:Get()
		SpellData:FindFirstChild(statname).Value = SpellsInStore[statname].Value
     end
 end

this is not working :sob:

2 Likes