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

No, but you could mix :AfterSave with an ordered data store to keep an up to date ordered data store.

1 Like

I have this code (basically identical to yours) however it just ignores the for loop for some reason.

function admin.Clear(player, userId, name) -- GDPR compliance (Code by Kampfkarren)
	if admin.CheckAdmin(player) then
		print("Starting to delete data")
		local orderedDataStore = DataStoreService:GetOrderedDataStore(name .. "/" .. userId)
		local dataStore = DataStoreService:GetDataStore(name .. "/" .. userId)
	
		while true do
			local pages = orderedDataStore:GetSortedAsync(false, 100)
			local data = pages:GetCurrentPage()
			print("Here")
			for _, pair in pairs(data) do
				print("Loop")
				print(("key: %d"):format(pair.key))
				dataStore:RemoveAsync(pair.key)
				orderedDataStore:RemoveAsync(pair.key)
			end
			print("end")
			if pages.IsFinished then
				print(("finished (%d: %s)"):format(userId, name))
				return
			end
		end
	else
		warn(player.Name.."tried to use admin commands")
		dataSaver.UpdateBehavoir(player, 20000000000000000) -- A non-admin tried to use admin commands, give perm ban
	end
end

This is my output:

err

And if I wanted to go about accessing a specific thing, say behavior points of an offline user how would I do this?

All in all DataStore2 is very nice to use, keep up the good work!

1 Like

Do you use combined data stores? What’s the name you’re giving it?

2 Likes

Yes, this is how I am doing it

dataStore2.Combine("DATA", "BehavoirPoints", etc.)
1 Like

And what name are you using? In your case, you should be clearing DATA.

3 Likes

Do you mean like this?

		local orderedDataStore = DataStoreService:GetOrderedDataStore("DATA" .. "/" .. userId)
		local dataStore = DataStoreService:GetDataStore("DATA" .. "/" .. userId)
1 Like

Yes, or just do admin.Clear(player, userId, "DATA")

2 Likes

:thinking: It loops through however the data is not reset to the default when I rejoin? This is also the case when you reset an offline players data.
For some reason it is only looping 2 times and there are 6 items in the combined store

dataStore2.Combine("DATA", "Gold", "Emerald", "BehavoirPoints", "LastLoginTime", "PlayedBefore", "LandDeedShopOpen")

err

I wouldn’t know–I’ve only ever used that in the command line. It’s possible DataStore2 is saving your data when you leave, overriding your deletion.

3 Likes

So after some tinkering, I think it has.something to do with :GetOrderedDataStore only liking numbers, and I had some book values. By making 2 combined stores, one for floats and ints and the other for other data types, it fixed the problem of not lopping correctly. As for the saving problem, that was completely my fault, I had made a typo on importing data from the data store this was importing the wrong data.

2 Likes

Hello! I love this datastore system, its so useful especially for mountains of data. I’m having a problem with a particular store… I have 5 seperate stores, four of which work perfectly and i’m very pleased… especailly because a few of them are carrying very full dictionaries. The one store i have however, the one where i save all of the players data regarding the character, is not working, i don’t think i’m doing anything differently from any of the other stores, but… it creates the data i need(I have checked this with print) and i try to save it into the store, and afterwords i check in the set store and it has the new value in it… so it should save, and then I leave and come back and all the data has reset. :confused:. I am using a key from an int value in the place where i keep all the datastore scripts so that i can easily reset all the stores if need be. I have tried changing the name of the store to see maybe if that store was corrupted of some sorts, there are no errors in the code from the server or client output, all the new fresh data arrives where i need it too as values in the player in their own folder. I’ve been scratching my head at this.

This is a copy of the code for the store itself(This isn’t very different from the other stores that are actually working and saving):

  local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local DataStore2 = require(1936396537)

local key = script.Parent:WaitForChild("Key").Value

Players.PlayerAdded:Connect(function(Player)
local CharInfoStore = DataStore2("CharInfoStore"..key, Player)

local CharacterStats = Instance.new("Folder")
CharacterStats.Name = "CharacterStats"
CharacterStats.Parent = Player

CharInfoStore:GetTable({
	["CharName"] = "";
	["CharDesc"] = "";
	["Aires"] = 30;
	["CurrentLand"] = "Bouldaron";
	["Gender"] = "Male";
	["Skin Tone"] = Color3.fromRGB(255, 204, 153);
	["Face"] = "Male 1";
	["Upper"] = "Violet Velvet Robes";
	["Lower"] = "Peasent Pants";
	["Hair"] = "None";
	['Head Wear'] = "None";
	["Back"] = "None";
	["BodyTypeScale"] = .03;
	["DepthScale"] = .8;
	["HeadScale"] = 1;
	["HeightScale"] = 1;
	["WidthScale"] = .8;
})

local CharInfoStoreGotten= CharInfoStore:Get()

for statname, statvalue in pairs(CharInfoStoreGotten) do
	if type(statvalue) == 'number' then
		local intvalue = Instance.new("NumberValue")
		intvalue.Name = statname
		intvalue.Value = statvalue
		intvalue.Parent = CharacterStats
	elseif type(statvalue) == 'boolean' then
		local boolvalue = Instance.new("BoolValue")
		boolvalue.Name = statname
		boolvalue.Value = statvalue
		boolvalue.Parent =  CharacterStats
	elseif type(statvalue) == 'string' then
		local stringvalue = Instance.new("StringValue")
		stringvalue.Name = statname
		stringvalue.Value = statvalue
		stringvalue.Parent = CharacterStats
	elseif type(statvalue) == 'userdata' then
		local stringvalue = Instance.new("Color3Value")
		stringvalue.Name = statname
		stringvalue.Value = statvalue
		stringvalue.Parent = CharacterStats
	end
end
for i,v in pairs(CharInfoStoreGotten) do
		print(i,"=", CharInfoStoreGotten[i], " In the Character Stats Store")
		CharacterStats:FindFirstChild(i).Value = CharInfoStoreGotten[i]
end
end)

I print whats coming out of the store so that i can easily see if the data did that stupid thing( i should preface this same store was working before, all of the blank stores were saving if they were blank and everything. :confused:

This is an example of some code thats trying to save the store:

     function Character_Appearance_Service:Character_Scaling(player,ScaleType,ScaleVal)
local CharStore = DataStore2("CharInfoStore"..key, player)
local CharStoreGotten = CharStore:Get()
local CharStats = player:WaitForChild("CharacterStats")
local ScaleTypeVal = CharStats:WaitForChild(ScaleType)
local Character = player.Character
local hum = Character:WaitForChild("Humanoid")
local humDesc = hum:GetAppliedDescription()
if ScaleType == "WidthScale" then
	ScaleTypeVal = ScaleVal
	CharStats:WaitForChild("DepthScale").Value = ScaleVal
	
	humDesc[ScaleType] = ScaleVal
	humDesc["DepthScale"] = ScaleVal
	
	hum:ApplyDescription(humDesc)
	print(ScaleType, "Is now being svaed in store as =", ScaleVal)
	CharStoreGotten[ScaleType] = ScaleVal
	CharStoreGotten["DepthScale"] = ScaleVal
	CharStore:Set(CharStoreGotten)
else
	ScaleTypeVal = ScaleVal
	
	humDesc[ScaleType] = ScaleVal
	hum:ApplyDescription(humDesc)
	print(ScaleType, "Is now being svaed in store as =", ScaleVal)
	CharStoreGotten[ScaleType] = ScaleVal
	CharStore:Set(CharStoreGotten)
end

end

function Character_Appearance_Service:Apply_From_Customize(player,Item_Name,Catagory)
  local Character = player.Character
  local CharStore = DataStore2("CharInfoStore"..key, player)
local CharStoreGotten = CharStore:Get()
local CharStats = player:WaitForChild("CharacterStats")
local CatName = CharStats:WaitForChild(Catagory)

print(Catagory, "Is saving the thing in the thing with the thing", Item_Name, CharStoreGotten[Catagory])
CatName.Value = Item_Name

CharStoreGotten[Catagory] = Item_Name
print("Is now:", CharStoreGotten[Catagory])
CharStore:Set(CharStoreGotten)

Character_Appearance_Service:Character_Apparel_Equip(Character,true)
end

I’d really appreciate your thoughts @Kampfkarren(You’ve always been very helpful) or anyone else who may have any ideas. Thx! :slight_smile:

4 Likes

I’m not exactly sure what your problem is and this is a LOT of code that I don’t have the time to read and dissect.

1 Like

I understand that. My problem is that the code won’t save for some reason, it says that it is in the store after i set the code in the store(And i’m not doing anything differently from any of the other stores and how i save them). I’m just not sure what could be wrong.

1 Like

During the little Roblox outage in the afternoon, I was trying to access a testing place of mine, which had DataStore2 for saving coin data. While it did increase and act normally in a session, when I rejoined, the coin data was set back to its value from the beginning of last session, rather than pick off from when I left. Is there any way for this to be prevented?

Code:

local startValue = 0
game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Wait()
	local CoinStore = DataStore2("Coins",  plr)
	CoinStore:SetBackup(10, 0)
	local Coins = Instance.new("IntValue")
	Coins.Name = plr.Name
	Coins.Parent = workspace.CoinData
	CoinStore:Get(0)
	local function updateCoins(newValue)
		Coins.Value = CoinStore:Get(newValue)
	end
	updateCoins(startValue)
	CoinStore:OnUpdate(updateCoins)
	CoinStore:AfterSave(updateCoins)
end)
1 Like

Because…

How do you expect DataStore2 to save the data if Roblox is down? :slight_smile:

3 Likes

True. However, the issue is now, when I’m entering the test place, even though Roblox is functioning now, the coin data starts from what it was last session.

Coin Data from beginning of Session 1: 3125
Spends 500 coins in-game, shows it at 2625.
Rejoins, coin data back up to 3125.

1 Like

It doesn’t look like you ever call :Set in your code, so that’d be something to look at.

After much hassle and research @Kampfkarren I have figured out my issue… It wont save the color3 value. After all of that, i finally found the issue. Now, can you just not save a color3? how would you do that?

I mean i understand its just the value of the color 3, but even then it would be a vector 3 value, or a userinput value

2 Likes

You can’t. The easiest way is to just use a table of RGB values.

Oh ok… and so when i load it into a value do it as in do it as in

(I have to figure out what type a table of like that would be)
elseif type(statvalue) == ‘userdata’ then
local stringvalue = Instance.new(“Color3Value”)
stringvalue.Name = statname
stringvalue.Value = Color3.new(statvalue[1],statvalue[2],statvalue[3])
stringvalue.Parent = CharacterStats
end

1 Like