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

I’m having some trouble with saving data.
I thought it was due to me not using combine properly but that doesn’t seem to be the case.

Does not save and does not show my leaderstats

You aren’t giving me nearly enough information to be able to help you. Please keep your replies as one post instead of repeating yourself.

1 Like

Here is my code it does not work

local DataStore2 = require(game.ServerScriptService:WaitForChild(“MainModule”))
– This line requires the Module script to allow us to utilize it’s API and functions (like :Increment(), :OnUpdate(), :Get(), :Backup(), etc.)

local defaultCoins = 10
– This variable sets the default coin value a new player will get upon joining the game for the first time.

game.Players.PlayerAdded:Connect(function(player) – Fires every time a player joins the game
local coinsDataStore = DataStore2(“coins”, player)
– Creates the ‘coin’ key for each player that joins the game, which can be named the same for every player as the Module Script [DataStore2()] handles making each key separate and unique for you.

local leaderstats = Instance.new("IntValue", player)
leaderstats.Name = "leaderstats"

local coins = Instance.new("IntValue", leaderstats)
coins.Name = "Coins"
-- Here we create the standard player values (without actually setting its value, we did that earlier with the defaultValue variable

local function coinsUpdate(updatedCoins)
    coins.Value = coinsDataStore:Get(updatedCoins)
end
-- This function updates the coins value

coinsUpdate(defaultCoins)
-- Once a new player joins the coinsUpdate function is fired, if no data is found on the key, then the (defaultCoins) variable we defined earlier will pass through, setting the players value to 0.

-- REMEMBER: Setting the coins value will not impact data saving, as the DataStore Value is what gets saved, the players value only reflects the value inside of the DataStore, we never fully write the players value  to the key (The DataStore2 module handles all data saving and cache writing)

coinsDataStore:OnUpdate(coinsUpdate)
-- Once the value is changed (using the module API like :Set() or :Increment()) then we use the :OnUpdate() method to fire the coinsUpdate function and update the players value to whatever the DataStore value has been updated to.

end)

game.Workspace:WaitForChild(“CoinPart”).ClickDetector.MouseClick:Connect(function(player)
– This function runs everytime a player clicks the brick in the workspace named “CoinPart”
– Once clicked, we increment the Value saved to the Key by (15) [add 15 to whatever the value is]
local coinsDataStore = DataStore2(“coins”, player) – getting that key to write the updated value to before making the change in the player.

coinsDataStore:Increment(50, defaultCoins)
-- This changes the DataStore value, which once updated uses the :OnUpdate() method above to then change the players coin value.

end

1 Like

Current version of DS2, which is this;
DataStore2.rbxmx (38.9 KB)

Causes Roblox Studio to freeze for 15-25 seconds after hitting stop, and then writes this;
Not running script because past shutdown deadline (x3)
No, it’s not anything from my code, it was alright before I downloaded DS2.

This is a Roblox bug that is in the process of being fixed.

1 Like

Your code isn’t formatted correctly for me to be able to read it. Can you use code tags?

```
– code here
```

local leaderstats = Instance.new("IntValue", player)
leaderstats.Name = "leaderstats"

local coins = Instance.new("IntValue", leaderstats)
coins.Name = "Coins"
-- Here we create the standard player values (without actually setting its value, we did that earlier with the defaultValue variable

local function coinsUpdate(updatedCoins)
    coins.Value = coinsDataStore:Get(updatedCoins)
end
-- This function updates the coins value

coinsUpdate(defaultCoins)
-- Once a new player joins the coinsUpdate function is fired, if no data is found on the key, then the (defaultCoins) variable we defined earlier will pass through, setting the players value to 0.

-- REMEMBER: Setting the coins value will not impact data saving, as the DataStore Value is what gets saved, the players value only reflects the value inside of the DataStore, we never fully write the players value  to the key (The DataStore2 module handles all data saving and cache writing)

coinsDataStore:OnUpdate(coinsUpdate)
-- Once the value is changed (using the module API like :Set() or :Increment()) then we use the :OnUpdate() method to fire the coinsUpdate function and update the players value to whatever the DataStore value has been updated to.

end)

game.Workspace:WaitForChild(“CoinPart”).ClickDetector.MouseClick:Connect(function(player)
– This function runs everytime a player clicks the brick in the workspace named “CoinPart”
– Once clicked, we increment the Value saved to the Key by (15) [add 15 to whatever the value is]
local coinsDataStore = DataStore2(“coins”, player) – getting that key to write the updated value to before making the change in the player.

coinsDataStore:Increment(50, defaultCoins)
-- This changes the DataStore value, which once updated uses the :OnUpdate() method above to then
1 Like

What’s the actual issue you are getting?

The Issues are

  • when i use this it does not show my leaderstats
  • It does not save the data whether be it on the game or studio
  • third i always get this error image
    you may know the third one.

leaderstats is not an IntValue, instead it is a folder:

local leaderstats = Instance.new("Folder", player)

I can’t see where you initiated the variable CoinsDataStore. below you can see the correct way to call a datastore for an unique player. use this variable in the Increment() function.

local CoinsDataStore = DataStore2("CoinsDataStore", player) --this way you create a datastore for the player 

Read this post:

1 Like

Thank you for reminding me :smiley:

Very helpful with that “SaveInStudio” value, however, is there a “LoadInStudio” equivalent? Let’s say I already have data that has been manipulated and is no longer the original data, how would I go about emulating new player data?

1 Like

If I’m not solo in a game and I use Data:Increment(), then it will give me and the other player the same amount.

Current Code
local NewData1

game.Players.PlayerAdded:Connect(function(Player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = 'leaderstats'
	leaderstats.Parent = Player
	
	local Points = Instance.new("IntValue")
	Points.Name = 'Points'
	Points.Parent = leaderstats
	
	local DataStore2 = require(script.DataStore2)
	NewData1 = DataStore2("PointsStorage",Player.UserId)
	
	print('Worked')
	
	local function UpdatedPoints()
		wait()
		Points.Value = NewData1:Get()
	end
	
	UpdatedPoints()
	
	wait(1)
	
	NewData1:OnUpdate(UpdatedPoints)
	
	UpdatedPoints()
	
	
	
end)

game.Players.PlayerRemoving:Connect(function(Player)
	NewData1:Save()
end)

game.ReplicatedStorage.RemoteEvents.CollectWater.OnServerEvent:Connect(function(Player,Water)
	Water.Transparency = 0.4
end)

game.ReplicatedStorage.RemoteEvents.DrinkWater.OnServerEvent:Connect(function(Player,Water)
	Water.Transparency = 1
	Water.Parent.Handle.DrinkSound:Play()
end)

game.ReplicatedStorage.RemoteEvents.GivePoints.OnServerEvent:Connect(function(Player,Amount)
	Player.leaderstats.Points.Value = Player.leaderstats.Points.Value + Amount
	NewData1:Increment(Amount,0)
	Player.leaderstats.Points.Value = NewData1:Get()
end)

game.ReplicatedStorage.RemoteEvents.Purchase.OnServerEvent:Connect(function(Player,Tool,Price)
	Player.leaderstats.Points.Value = Player.leaderstats.Points.Value - Price
	Tool:Clone().Parent = Player.Backpack
	NewData1:Increment(-Price,0)
end)

image

I know this isn’t the best place to ask for assist, but maybe just a quick exception?

No, there is nothing like this as of yet.

There’s no way for me to help you without seeing the code that calls :Increment, the context for it, etc, as well as knowing where this script is.

I managed to code something that refers to a default data module. It’s a bit messy but I am not exactly the best at coding!

image

1 Like
  1. You’re going to hit throttles on that because you’re not combining.

  2. I don’t see where :Increment is called, and I still don’t know where this script is. Notably, there are known issues when DataStore2 is only ever referenced in a StarterCharacterScript.

The code displayed is from part of a PlayerAdded event.

image

The combine is before the PlayerAdded event.

image

But please correct me if the way I am using this is not appropriate.

Apologies about what I said before, I confused you with the other person.

Yes, you are using it correctly.

1 Like