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

Refactor testing!

I’m working on a big code refactor. The point right now is just to make the code cleaner, you should not have to change any code.

I would really appreciate if existing DataStore2 users could test this version and make sure everything works exactly like it did.

Once any issues are identified, I’m going to publish this on the main free model and hopefully the package works this time.

Edit: Not sure why, but it’s not for sale. Oh well.

7 Likes

I am a bit confused on backups. I have my data working just fine, saving and loading and now am looking to implement the backups.

coinStore:SetBackup(3)

If it reaches the maximum amount of retries, does it just prevent data from saving? Also it says it will set the value of the data store to the value provided. Does that mean if the game fails to get its data X amount of times, their data will become nil if no value is passed? Confused I am.

3 Likes

Yes, and treats :Get() like the player didn’t have data.

No, because it won’t save. :Get() will return nil though if you don’t provide a default value AND it fails.

3 Likes

Can i transfer my existing datastores to datastores 2?

1 Like

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.