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

  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

???
What do you mean? I have my full script with Increment and a picture of where the script’s located.

Apologies, your code was hidden so I didn’t see it.

You are using DataStore2 completely wrong–it’s likely you have an outdated version, since you would’ve been alerted otherwise:

You are not meant to use their player ID.

You also should not ever yield in an OnUpdate, but there’s no reason you should be doing that in your case anyway (that wait() doesn’t do anything).

Sorry, that was a script from yesterday. I’ve fixed some problems but the Increment() giving points to a player gives points to all players.

New Code
local p = {}

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(1936396537)
	p[Player.UserId] = {NewData1 = DataStore2("PointsStorage",Player)}
	--NewData1 = DataStore2("PointsStorage",Player)
	
	local function UpdatedPoints()
		Points.Value = p[Player.UserId].NewData1:Get()
	end
	
	UpdatedPoints()
	
	p[Player.UserId].NewData1:OnUpdate(UpdatedPoints)
		
end)

game.Players.PlayerRemoving:Connect(function(Player)
	p[Player.UserId].NewData1:Save(Player)
	wait(4)
	p[Player.UserId] = nil
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
	p[Player.UserId].NewData1:Increment(Amount,0)
	Player.leaderstats.Points.Value = p[Player.UserId].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
	p[Player.UserId].NewData1:Increment(-Price,0)
end)

The only thing I can guess is that your code for drinking is firing whenever any player drinks. Either way, it is not a DataStore2 bug.

I’m sorry if this seems like a dumb question, but I’m a bit confused on how combining data keys works. Does it save data under a table? How do I access said data later then?

I noticed that the GetTable function has support for data that is added on later after the data is saved. Do I use this with Combine?

For example, say that here’s the data I want to use…

{
    Cash = 100
    Locations = {Japan=5,...}
}

(Might change this in the future I just want an example to work off of)

How would I set this up, using combine data stores, GetTable, etc, In a way that I can possibly add/remove data in the future without worrying about if statements to check if the player has specific data? Once again apologies if this is a dumb question I’m just not sure how to use Combine with other APIs provided and I’d appreciate clarification.

Please read the thread.

That’s not what I meant. I was referring to how the Combine function actually works/how I retrieve data after combining data stores. I am very confused as to how Combine actually works.

You’re not really meant to think about that, you just use Combine for the keys and use DataStore2 as you normally would.

DataStore2.Combine("DATA", "Cash")

-- later
print(DataStore("Cash", player):Get(0))

If you want to use this table as the default data, you can just loop over it and combine them all.

I think I understand, thank you for the clarification

1 Like

I keep getting the error “Not running script because past shutdown deadline” when using DataStore2 in studio which causes the studio to freezes whenever I stop a testing session, is there any fix to this?

I require the the module using direct ID: DataStore = require(1936396537)

Hey there! :slight_smile: I remember some plans for an auto-combine feature, has that been implemented yet?

As said several times before in the thread, this is a Roblox bug that is getting fixed soon.

It has not, I haven’t had much time to work on it.

1 Like

Ohh so that how, i never knew this studio feature like that… :upside_down_face:

1 Like

Hey @Kampfkarren why cant i just do it on another script for example i made the DataStore2 in my leaderstats and if i do it on another i always get an error i made a solution but the coins are 0 in value my blades are in 75 for example then i exchange it becomes 0 coins?

Hello, @Kampfkarren how I can access the offline players’ data with DataStore2 when I am in the game?

This could be a feature in the future. Currently DataStore2 has no way to retrieve offline player data.