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

No, somewhat intentionally. The data loss prevention is so that setting/getting does not have data loss attached to it, not for something if you screw up. Also there’s no great way to write a generic API for it. You would have to modify the module.

1 Like

I see. I always like to have the option to revert data available for my admins, but i’ll re-evaluate how needed that is and try editing the module if necessary.

2 Likes

That’s a fair use case, I think. There may or may not be an issue on the GitHub page for this but if there isn’t please make one.

1 Like

Is there any way to save datastore without the instance of a player?

4 posts were merged out for being off-topic

Please refer to the thread linked below

@DevAndero @ObstacleIsland

2 Likes

Is there a way to store purchase history with this?

2 Likes

Per player? Easily, just save a table for them. Globally? No, and I probably wouldn’t recommend data stores for that anyway because of their finicky nature when multiple servers mutate the same key.

3 Likes

Thank you, you’re always so helpful.

Probably a dumb question but how should I detect if I’m running on backup data? If I do this, will it yield until it’s finished calling GetAsync 5 times and then print whether or not the datastore is a backup?

local plrDatastore = DataStore2(plr)
plrDatastore:SetBackup(5,funcs.defaultValue)
print(plrDatastore:IsBackup())

Your code is wrong (no key, you don’t call :Get()), but the general idea of this is correct, though it won’t try to call GetAsync until you actually call :Get().

1 Like

Oh I see, so to tell the player they’re running on a backup and their data won’t save, I should do something like this?

local plrDatastore = DataStore2('key',plr)
plrDatastore:SetBackup(5,funcs.defaultValue)
local plrData = plrDatastore:GetTable(funcs.defaultValue)

if plrDatastore:IsBackup() then
--notify player that their data will not save
end

Yeah, but the funcs.defaultValue in your SetBackup isn’t necessary–:Get() (and by extension :GetTable()) should use the default value you provide.

1 Like

Alright I see. Thank you for the help and quick responses.

2 Likes

Hey, You may of already said something about this but is there a way to reset a players data?
I was just wondering because of I’m making a story/adventure game where after you complete the story you can reset data and start over again like you are a new player.

2 Likes

You can just call :Set(nil) on all the keys you use, I believe.

4 Likes

There is documentation on datastore2 to make it work in studio. By default it does not.

2 Likes

First I just wanted to thank you for this - I’m using it for a game I’m working on and it works like a charm!

Is there any way to specify certain stats to save or indicate certain stats not to save? I’m sort of confused if all stats in the leaderstats are automatically added to the database.

This is my set up right now:

DataStore Set up

The script works, I just am confused where it knows to only save the “Shillings” leaderstat vs all leaderstats or whether it just saves all leaderstats unless explicitly told not to.

local DataStore2 = require(game.ServerScriptService:WaitForChild("MainModule"))
local FunctionModule = require(game.ServerScriptService:WaitForChild("FunctionModule"))
local DefaultValue = 0

game.Players.PlayerAdded:Connect(function(Player)
	local ShillingsDataStore = DataStore2("Shillings", Player)
	FunctionModule.LeaderstatsCreate(Player)
	FunctionModule.LeaderstatsCategory(Player,"Shillings","IntValue")
	local Shillings = Player.leaderstats.Shillings
	local function ShillingsUpdate(UpdatedValue)
		Shillings.Value = ShillingsDataStore:Get(UpdatedValue)
	end
	ShillingsUpdate(DefaultValue)
	ShillingsDataStore:OnUpdate(ShillingsUpdate)
end)

Sorry if this was already asked before and thank you again!!

1 Like

I am not sure but is it possible to use a player ID instead of a player instance for saving?

As answered several times before in this thread, DataStore2 only uses player instances for the time being.

1 Like