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

DataStore2 doesn’t provide a way to do this, but it’s not hard to do by hand.

2 Likes

For anyone who wants to stay up to date on DataStore2, I’ve now made a thread that you can watch to get notifications without having to watch this thread.

3 Likes

I do have to say, I love this post, this is a great asset for my game. It completely changed the way my data-saving works and has seasoned my skills with proper data-store usage. After fiddling around with it, I figured out how to implement it into my new upcoming game. Bravo, and great job! Keep up the excellent work :slight_smile:

1 Like

Nevermind, I misnamed a variable.

I’m attempting to use DataStore2 in a new game of mine but I keep getting the following error:

ServerStorage.DataStore2:351: attempt to perform arithmetic on a table value
Stack Begin
Script 'ServerStorage.DataStore2', Line 351 - function Increment

this is what I’m calling:

local stars = DataStore2( "Igc", player )
stars:Increment( stars )

Edit:
The problem persists even if I require the module directly.

1 Like

That means the value of stars:Get() is a table. What happens when you print(stars:Get())?

Did have a quick question, when adding new data to my save table. It would make that data nil obviously. But the only way to get around this is resetting the data store. Is there a method where I could possibly add new data without it being set tot nil?

I had a function for my old datastore like this:

function LoadNewData(Player)
    local Default = blankData.ReturnBlankData()

    for Key,Value in pairs(Default) do
        if sessionData[Player.UserId][Key] == nil then
            sessionData[Player.UserId][Key] = Value
        end
    end
end

Not sure if it’s good enough for this datastore though.

1 Like
1 Like

Ahh, I’m silly. Appreciate it lol.

1 Like

So, I’m adding a new table and using the GetTable method, but when I remove that table from the default data it still seems to be there. Anyway that I can get rid of it?

What is the exact inputs/outputs you’re expecting? You want to have data in a data store…but only temporarily?

1 Like

So, heres my default data table:

local function setupUserData()
	local userData = {
		Currency = {	
			["Money"] = 100000;
		};
		
		ownedPlacements = {};
		plotPlacements = {};
	}
	
	return userData;
end

let’s assume that I add a new table, “testTable = {“test”};”

I join the game and loop the data and “test” is in it. But let’s remove that table from my default data table, whenever I rejoin the contents of that testTable will still be in there for some reason.

1 Like

That’s too niche of a case to support in the API. Why not just remove it yourself?

That’s the issue? I can’t just remove it because it will still show as a valid content of the table?

local userDataStore = DataStore2("userData", player)
local userData = userDataStore:Get()
userData.test1 = nil
userDataStore:Set(userData)

Am I missing something here? Why wouldn’t this work?

I didn’t say that wouldn’t work? but wouldn’t there be some automation with this if I had a large player-base and don’t wanna have to go to studio and make a table nil, then proceed to save the game and shutdown the entire game?

I suppose i’ll just add something to the datastore module.

Anything DataStore2 would provide would just be the same thing, so I’m not sure what could be introduced to help with this. If you used combined data stores (which you should be), I want to eventually introduce a way to clear a combined data store.

I don’t have a combined datastore?

It’s the equivalent to your whole user data thing only it does it in a more ergonomic fashion.

I’m currently just using something like this:

local function setupUserData()
	local userData = {
		Currency = {	
			["Gems"] = 15;
			["Coins"] = 1500;
			["Points"] = 50;
			["lol"] = 50;
		};
		
		Items = {};
	}
	
	return userData
end

and then have this

local userData = dataStore2(userDataStoreName, player):GetTable(setupUserData());

print(userData.Currency.Money)
1 Like

Combined data stores are the equivalent to that but more ergonomic and are prioritized in development.

As in, you do DataStore2("Gems", player) instead of having to go through the data table, but it still all saves as one.

1 Like