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

Wow! Ive been looking for something like this for a long time, I’m so glad you made this thread. I can’t wait to give it a shot for myself. :slight_smile:

1 Like

Still Confused :confused: and my game releases in a week

Internally, yes, but you should never use the master key (“DATA” in your case) anywhere outside DataStore2.Combine.

To access stores from multiple scripts, just call DataStore2 again.

You are required to use your own ordered data stores.

1 Like

You can forcefully add a :BindToClose in another script, but it is very unlikely it’s not long enough.

1 Like

The problem is with your code, not mine. Please read your errors, and the lines at which Roblox is telling you it is coming from.

1 Like

No idea why it is happening? Could it be something wrong with my :Set()? Can something be erroring when it’s saving like if I didn’t set something correctly

It’s not studio. Some players rejoin my game and their data has reset but it only happens sometimes

Heyo, I am trying to use DS2 with Rojo, Aero Game Framework and Visual Studio Code. When I download the files from github and extract, the DataStore2 file is actually just a folder, and not a script as it should be. When I drag it into my file structure on my PC, it comes in as just a folder instead of a script with scripts nested under it.

Only way I can convenience to do this is manually copy and paste each script from Roblox.

Surely I am doing this wrong. Noob problems.

I use DataStore2 with Rojo easily, use Git submodules instead of cloning, then (assuming it’s in a folder called vendor/DataStore2), put this in your project file:

        "DataStore2": {
          "$path": "vendor/DataStore2/DataStore2"
        }
2 Likes

How can I use DataStore2 with AeroGameFramework? I keep getting this error.

16:03:18.936 - ServerStorage.Aero.Services.LevelService:67: attempt to call a table value
16:03:18.936 - Stack Begin
16:03:18.937 - Script 'ServerStorage.Aero.Services.LevelService', Line 67 - function characterAdded
16:03:18.937 - Script 'ServerStorage.Aero.Services.LevelService', Line 74 - function playerAdded
16:03:18.938 - Stack End

I’m using this code to call DataStore2.

local currentLevel = DataStore2("level", player):Get(1)

and to set the DataStore2 variable I use this.

function LevelService:Init()
	DataStore2 = self.Modules.DataStore2
end
1 Like

I think someone has done it before, it’s because AGF doesn’t copy over the metatable. I forget the solution to this.

2 Likes

Hey, I’m using DataStore2 and it’s great. No reports of data loss with over 500K visits.

I have one question though. If I wanted to implement VIP servers into my game as a sort of sandbox mode, how could I make DataStore2 just not save any data from VIP servers without adding checks to every line of my code that sets DataStore2 values?

I was thinking something like this in the main module of DataStore2:

function DataStore:Set(value, _dontCallOnUpdate)
	if serverType ~= "VIPServer" then
		self.value = clone(value)
		self:_Update(_dontCallOnUpdate)
	end
end

It would check what type the server is, and if it’s not a VIP server then :Set would work normally. If it is, then :Set just wouldn’t do anything. Is this a viable way of doing it to guarantee DataStore2 doesn’t overwrite any data in VIP servers? I don’t use :Update or :Increment, only :Set. I just want to be sure so I don’t accidentally overwrite everybody’s data. Thanks!

1 Like

You could probably just hook :BeforeSave and make it return the original value.

This feels like something too niche to include in the upstream.

2 Likes

Would I have to do that for every key or just the master key?

Does BeforeSave just set the value in the DataStore to whatever the modifier is? That’s what I think it does from looking at documentation and the module, but I’m not sure. Thanks

You should never use the master key anywhere other than DataStore2.Combine.

Would something like this work?

	if serverType == "VIPServer" then
		for i,v in pairs(keys) do
			local store = ds2(v, p)
			local data = store:Get()
			store:BeforeSave(function()
				return data
			end)
		end
	end

This is ran as soon as the player joins the game.

I’m currently in the process of thoroughly testing it myself. I wanted to ask you to see if you notice any issues with it right off the bat, since you’re more familiar with the module than I am. Thanks again

2 Likes

That should probably be fine, though again I’ve never done something like this before.

1 Like

This is work very well, but i still have a problem with my leaderstats and what i intend to.

— From Client

From the client is two different set, I Click button to add value into leaderstats but drawn together?

Local Script

local plr = game.Players.LocalPlayer

local rs = game:GetService(“ReplicatedStorage”)

local Send = rs.Add:WaitForChild(“PointEarn”)

script.Parent.MouseButton1Click:Connect(function()

Send:FireServer()
print(“Value add sucessfully”)
end)

Local Script2

local plr = game.Players.LocalPlayer

local rs = game:GetService(“ReplicatedStorage”)

local Send = rs.Add:WaitForChild(“Ref”)

script.Parent.MouseButton1Click:Connect(function()

Send:FireServer()
print(" Cash Value Order")
end)

Script from ServerScriptService

local DataStore2 = require(1936396537)

local defaultValue = 0

local defaultValue2 = 0

local rs = game:GetService(“ReplicatedStorage”)

local Send = rs.Add:WaitForChild(“PointEarn”)
local Send2 = rs.Add:WaitForChild(“Ref”)
---------------------------------------------- End of Title Variable

game.Players.PlayerAdded:Connect(function(plr)

local pointDataStore = DataStore2("points",plr)
local pointDataScore2 = DataStore2("points",plr)



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

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

local cash = Instance.new("IntValue",leaderstats); cash.Name = "Money"

---------------------------------------- Save Player/Load Player
local function pointUpdate(UpdateValue)

 points.Value = pointDataStore:Get(UpdateValue)
 cash.Value = pointDataScore2:Get(UpdateValue)
	
end

pointUpdate(defaultValue,defaultValue2)

pointDataStore:OnUpdate(pointUpdate)
pointDataScore2:OnUpdate(pointUpdate)
---------------------------------------- End Player Data

------------ Trigger from Client 
Send.OnServerEvent:Connect(function()
pointDataStore:Increment(500,defaultValue)
print("Add sucessfully done!")
end)

Send2.OnServerEvent:Connect(function()
pointDataScore2:Increment(50,defaultValue2)
print("Cash add sucessfully done!")
end)	
------------ 	End

end)

It Work well and i haven’t receive any error but watch carefully, It drawn together when i click both button to add value from the leaderstats and it suppose to be separately. It does save well thank for that, but the leaderstats it added from the client with different buttons. Need help there i couldn’t figure it.

The free model in Roblox is no longer maintained. Get the updated code in the GitHub in the OP.

Thank you very much and I’ll probably won’t have any further problem.

@incapaxx