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

If you already have a leaderstat, then just use Changed. It’s replicated either way.

1 Like

Thank you for the help! Now that I know AfterSave is a thing, I’m going to revert the module back to it’s unmodified state.

Can’t @Crazyman32 's plugin be used with this?

Doesn’t DataStore 2 save data with some sort of scope because of its saving methods?

Image

image

why wouldn’t the name be DataStore2?

It can. If you searched “datastore2 data store editor” you would have found a solution. The data store name is dataStore2.Name .. "/" .. dataStore2.UserId so that means if I join your game and your game has "coins" data then you would do "coins/129574502"

1 Like

@sjr04

what scope would I use?

I couldn’t find any results for editing the data , though I did for how to clear.

Image

image

nevermind this is an excellent solution that worked :

If I shut down servers, does the datastore still save?

yes it saves automatically when the Server shuts down I think

1 Like

Can you call this from multiple different scripts or modules, and change data without them conflicting with eachother?

As answered several times before, yes you can.

I have a question regarding new servers compared to old servers.

Say I have .combine with several of my datastores, on update 1.0 I have “Data1” and “Data2”, then say in update 1.1 I add data store “Data3” to the same .combine as “Data1” and “Data2”. Then say someone join a new server with 1.1, obtains data for “Data3” then leaves, then joins an old server on update 1.0 still, would their previous Data3 be overwritten and removed? Or would it only be accessible in new servers? Would this lead to an issue where it’s necessary to shut down old servers? Or could I just use the migrate to new servers option and not worry about it? Sorry I don’t complete understand how the module works.

If they join an old server, Data3 will simply be ignored.

2 Likes

How would I go about accessing data of a player that isn’t in game? I’ve been messing around with the __call function in the main module, but i can’t seem to wrap my head around this.

I wish to access data of another player regardless if they are in game or not because i wish to implement a duos system where players can partner up with another player and this data saves, mainly the name of the partnering player and their user ID. But of course I will give players the option to disband this partnership. But the issue comes when both players aren’t in the same game. So if one player in another game or is offline gets disbanded by their partner, is there a way to detect this almost as if its live? If not, then a refresher every 30 seconds would check the status of the partnership.

Or am I being to ambitious and this would in fact not be an ideal system to make.

1 Like

Ever since we switched to DataStore2, my game has been experiencing detrimental dataloss. The only other ODS we use is for a donation leaderboard. We do not have many donations so I don’t see why that would be a huge issue. At this point, I have no idea why data is not saving. We are using combined datastores, and only 2 keys. We use PlayerData:Get(0) and PlayerData:Increment(x). I don’t see how that could cause any issues.


As a rewrite, I was planning on using this for our data (because I read that one key was better than 10 keys). Is this a good way to do it?

local DataService = require(ServerSettings.DataStore2)
DataService.Combine("Chicken_Shack_Version_Two", "Player_Data")

function GetData(Player)
	local PlayerData = DataService("Player_Data", Player)
	local InitialData = PlayerData:GetTable({
		["Points"] = 0;
		["Bux"] = 0;
		["Experience"] = 0;
		["Rebirths"] = 0;
		["PreviousQuest"] = nil;
		["LastJoinTimestamp"] = nil;
		["Housing"] = {
			["Plans"] = {};
			["Furniture"] = {};
		};
		["Fishing"] = {
			["Rods"] = {};
			["Backpacks"] = {};
		};
		["Settings"] = {
			["HighQuality"] = true;
			["MusicOn"] = true;
			["TagsOn"] = true;
			["AutoUniforn"] = true;
			["GenderMale"] = true;
		};
	})
	return InitialData
end

function PlayerUpdated(Player, Value)
	ServerEvents.Data.DataUpdated:Fire(Player, Value)
	ClientEvents.Data.DataUpdated:FireClient(Player, Value)
end

game.Players.PlayerAdded:Connect(function(Player)
	local PlayerData = DataService("Player_Data", Player)
	local InitialData = GetData(Player)
	
	PlayerUpdated(Player, InitialData)
	PlayerData:OnUpdate(function(Value)
		PlayerUpdated(Player, Value)
	end)
end)

Thanks :slight_smile:

3 Likes

So you need to get the current key, then from there, index the players datastore with that key.

2 Likes

Love the module, it makes life generally so much easier.
Other than the well known “no offline access” issue which should probably be builtin at some point, my only problem with the module is that it does not seem to work properly when executed from the command bar.
After thorough testing, requiring and getting cached data works from all server scripts, but always fails to return the cached data from the command bar, instead returning the datastore value. This happens when the Datastore2 module is accessed from the command bar directly, or by another module that is accessed by the command bar.
This is not critical because it only affects Studio testing, but it is something you should look at, if even fixable.

1 Like

I’m not sure what the use case for using the command bar with DataStore2 is.

1 Like

One would be manually running a module script that uses DS2 from the command bar and perhaps print it’s output to test it’s functionality.

1 Like

So not sure exactly what the problem is or if this is even a good way to use the :Set() function.

So basically I made a few Values for Kills, Wins, Losses etc. I then made a .Changed function on all of them and inside those .Changed Functions it does the :Set() function. Now when I change my values using the Console it changes and saves fine but whenever the game changes the values. They change but don’t save? This has really confused me.

Has anyone else experienced this?

1 Like

I’m not sure what you’re referring to, though the way you should structure your code is that everything that changes stats just calls Set through the data stores, and the leaderstats just use OnUpdate on the data store. You shouldn’t be modifying the leaderstats directly.

1 Like