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

How would I use this with soft shutdown? Whenever I attempt to saveall then do the tp, the player just disappears but never gets teleported so its just a regular shutdown
Edit: apparently there is a bind to close function in the module but never found it in the api
Edit2: apparently saveall changes the player ancestry which when i commented allowed me to actually tp properly

1 Like

Only the Coins part work for some reason Code1 doesn’t save. Also is it possible to save Bools in a leaderstats type thing?

local Players = game:GetService(“Players”)
local ServerScriptService = game:GetService(“ServerScriptService”)
local Workspace = game:GetService(“Workspace”)

local DataStore2 = require(ServerScriptService.DataStore2)
DataStore2.Combine(“DATA”, “Coins”)
DataStore2.Combine(“DATA”, “Code1”)

Players.PlayerAdded:Connect(function(player)
local CoinsStore = DataStore2(“Coins”, player)
local Code1Store = DataStore2(“Code1”, player)

local leaderstats  = Instance.new("Folder")
leaderstats.Name = "leaderstats"

local TwitterCodes = Instance.new("Folder")
TwitterCodes.Name = "TwitterCodes"

local Code1 = Instance.new("NumberValue")
Code1.Name = "Code1"
Code1.Value = Code1Store:Get(0)
Code1.Parent = leaderstats

local Coins = Instance.new("NumberValue")
Coins.Name = "Coins"
Coins.Value = CoinsStore:Get(0) -- Amount they will have by default
Coins.Parent = leaderstats

CoinsStore:OnUpdate(function(NewCoins) -- This function runs every time the datastore changes
	Coins.Value = NewCoins
end)

Code1Store:OnUpdate(function(NewCode1)
	Code1.Value = NewCode1
end)
leaderstats.Parent = player
TwitterCodes.Parent = player

end)

1 Like

instead of doing that i think you should just use

DataStore2.Combine("DATA", "Coins", "Coins1")
2 Likes

Hey, I’m currently willing to use this method and it works super fine + now that I understood how it works, I can work pretty fast with it. I have just one question: to look into players’ data, I use a plugin named DataStoreEditor (DataStore Editor - Roblox), BUT with DSS2 I don’t get what’s the actual key to find this data. I tried using the key put after DSS("Data",player) (Data is the key) but it doesn’t seem to be finding any data. Might be a really dumb question, but since I started using it a couple days ago, I’m not sure on how to find players’ data, or even if it’s possible. So, how can I find the data, possibly with that plugin?

1 Like

How to use DataStore2.ClearCache() ?

when i try to wipe all data to test everything it gives an error
Screenshot_250

Screenshot_251
its normal script

im using module inside serverscriptservice and not require(1936396537)
How can i wipe all data to test everything?

OH NEVERMIND Datastore2 is a module and i should require it lol. My bad :upside_down_face:

The key is
KEY/USER_ID

But read this to learn More and special thanks for @ MFCmaster1234

2 Likes

Awesome! Thanks for this! :smiley:

This amazing module has saved me so much time and effort with saving data, and the fact that it is so easy to use and how it just works makes it even better.
I feel like I’m writing a review or something, but I just had to express how awesome DataStore2 is…
Thank you so much for this!

I found myself unable to save data or load data from offline players because of that, also I dont quite understand why you made it so you have to use a player instance in the first place rather than just the ID of the player?

Spent some time learning how to use it and was worth it. Though I have some questions.

It’s regarding combining ‘keys’.

DataStore2.Combine("PLAYER_DATA", "Points", "Level")

Above is a line of code I made. So are the second arguments stored inside a key, which is ‘PLAYER_DATA’ in this case?

Meaning are Points and Level stored in PLAYER_DATA?

And If I were to change the name of the key that stores Points and Levels, will it reset the value associated with them?

How does it stop data throttling? ( heard from people around )

The Touched event does not return the player that touched it, it returns the part that touched it.
To get the player, you should use game.Players:GetPlayerFromCharacter(character)

game.Workspace.Path2.Touched:Connect(function(hit) -- hit is the part that touched it
    local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- it can return nil if hit.Parent isn't a player's character.
    if player then -- checking if it's a valid player
        local TokenData = DataStore2("Tokens", player)
        TokenData:Increment(10)
        print("Tokens increased by 10!")
        wait(10)
    end
end)

Also assuming you’re using wait(10) to make sure the event doesn’t fire too much, events don’t yield.
If you’re using wait(10) to stop the event from firing too much, use debounces instead.
The link to how to use debounces is here.

Hi, I tried it but it had the following output
22:20:17.986 - Model.MainModule:249: attempt to perform arithmetic (add) on nil and number
Did I not enable something? Because I recently transfered the game from one of my old groups to a new one.

Hello @Kampfkarren

I noticed some players joined in-game that their data doesn’t load at all.
How to solve that?

you probably missed what @lordmochibi said

game.Workspace.Path2.Touched:connect(function(Hit)
   local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
   if not Player then return end

   --Write your code here

end)

Please help me, I get an error with Update()

Yes.

Because it only has to load the data from PLAYER_DATA. Not multiple data stores.

Yes.

This is a problem with your code. I can’t really give much more help than that.

1 Like

This is a problem with your code. I can’t really give much more help than that.

My code:
Main Script
Server.PlayerDataHandler:StartPlayerData(player)

PlayerDataHandler

function PlayerDataHandler:StartPlayerData(player)
--Code
end

Can you give me examples that I might able to found a bug?

Is there like a call limit inside DataStore2 like the Regular DataStore?

Unless you’re using :Save, no.

Ok, and if I were, what would be the limit?