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

You can still use it for it, it actually works, i figured it out later down the line though. You simply juse use the same key you used to save the data with, for example DataStore2(“Coins”, player) then to get that data from an ordered datastore, I’d simply call “Coins” and then whenever that data updates it’ll update on the leaderboard

That’s not using DataStore2 for ordered leaderboards, that’s using DataStore2 and ordered leaderboards.

local ToolsDataStore = DataStore2("tools",player)
	
	local function UpdateTable()
		
		local NewTable = {}
		
		for i,v in pairs(player:WaitForChild("Backpack"):GetChildren()) do
			table.insert(NewTable,{
				Tool = v.Name,
				ToolOwned = v:WaitForChild("ToolOwned").Value,
				ImageId = v:WaitForChild("ImageId").Value,
				InventorySlot = v:WaitForChild("InventorySlot").Value,
				Stack = v:WaitForChild("Stack").Value,
				MaxStack = v:WaitForChild("MaxStack").Value
			})
		end
		print(NewTable)
		ToolsDataStore:Set(NewTable)
	end
	
	player:WaitForChild("Backpack").ChildAdded:Connect(UpdateTable)
	player:WaitForChild("Backpack").ChildRemoved:Connect(UpdateTable)

Whenever I try to set the table I just get this error:

DataStore2:518: attempt to call a table value - Server - DataStore2:518

And I have no idea how to fix it… Anyone have any solutions?

Line 518 is not anything that could give that error, make sure you are on the most up to date version (NOT the free model, which is no longer supported). What is on that line?

Just got the newest version from github. Now the error is on line 413. The whole error looks like this if it is to any help:
image

Looks like you have an OnUpdate function somewhere that is a table instead of a function. Using breakpoints here would help you figure out where you could be doing that.

You are correct. I had an OnUpdate that called a value instead of a function. Thank you so much :slight_smile:

1 Like

Does anyone know how I can reset/wipe all data saved by this module?

No

You probably want this: How to use DataStore2 - Data Store caching and data loss prevention - #187 by Kampfkarren

@Kampfkarren

Edit: nvm. I managed to figure it out.

I’ve switched from OrderedBackups to standard. I am unable to remove data for standard.

I’ve tried the code block you created in the case of a RightsOfErasure request, but it isn’t working with the Standard setting.

DataStorage:OnUpdate() became deprecated, does this mean it will became broked in the future? I can’t use Cross Server Messaging Service due to it characters limit

DataStore2 has never used this function because it never worked. :OnUpdate in DataStore2 is its own thing that only reacts to changes made in the place.

2 Likes

So is :OnUpdate() work if i update data on different server?

No

111111111111111111111111111111111111111111111111111111111111111111111

I am trying to get a callback for saved data once a player leaves the game. I only need 1 callback instead of a callback for each key

After doing some research about the :AfterSave feature and I couldn’t seem to find a solution for my problem.

Does :AfterSave only work specifcally on keys and not masterKeys?

If this is the case, I assume the best practice is to listen to one key instead of every key that I have, right?

AfterSave should not be used with master key, nothing should be except Combine. It is fine to listen to as many keys as you want as long as its ergonomic to you.

why is there orange underlines?

code snippet:

		local dataStore = DataStore2(combinedDataStoreInfo[dataStoreName], player)

		dataStore:BeforeSave(function(combinedData)
			for key in pairs(combinedData) do
				if combinedDataStoreInfo[key] then
					local combinedStore = DataStore2(key, player)
					local value = combinedStore:Get(nil, true)
					if value ~= nil then
						if combinedStore.combinedBeforeSave then
							value = combinedStore.combinedBeforeSave(clone(value))
						end
						combinedData[key] = value
					end
				end
			end

			return combinedData
		end)

Did you define DataStore2? Where did you define it?

it’s in the main module in the code

You have to require the DataStore2 module…

local DataStore2 = require(game.ReplicatedStorage.DataStore2) -- or where ever ur datastore2 module is