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

It isn’t data, read it again.
“These two non-datastore variables are displayed and updated in real time”
And they can’t be data because
“however they both change really quickly and frequently, so I can’t just check if these values are bigger than the stored ones each HeartBeat:Wait().”
To add a bit to this, saving them that frequently is not efficient either.

Edit: They are saved as data on player death, and I’m trying to achieve it to save on player quitting, however for some odd reason DataStore2 has this odd rule of not using :Set() on PlayerRemoving.

I’m trying to get the health and position of a player when they leave the game. I know with datastore2 you’re not supposed to save when the player leaves but I need the health and position of the player when they stop playing. Reading the documentation on datastore2 saving when the player leaves should work. But neither their health or position is saved?

local DataStore2 = require(game.ServerScriptService.DataStore2)
DataStore2.Combine("PlayerStats","Position","Health")
game.Players.PlayerAdded:Connect(function(plr)

	local PositionDataStore = DataStore2("Position",plr)
	local HealthDataStore = DataStore2("Health",plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	
	char.Humanoid.Health = HealthDataStore:Get(100)
	PositionDataStore:Get({workspace.Spawn.Position.X,workspace.Spawn.Position.Y,workspace.Spawn.Position.Z})
	char.HumanoidRootPart.CFrame = CFrame.new(PositionDataStore:Get()[1],PositionDataStore:Get()[2],PositionDataStore:Get()[3])
	
	plr.CharacterRemoving:Connect(function(SaveChar)
		HealthDataStore:Set(SaveChar.Humanoid.Health)
		PositionDataStore:Set({SaveChar.HumanoidRootPart.Position.X,SaveChar.HumanoidRootPart.Position.Y,SaveChar.HumanoidRootPart.Position.Z})
	end)
end)

Looking back now me and @Zorutan have similar issues.

EDIT: it appears using the model version of datastore2 fixed the issue? Weird, I thought the updated version was in github

How do I save tables? Trying to save tools with DataStore2.

Please search the forums or the thread where I say numerous times it’s the exact same as everything else.

The GitHub version is up to date and the model has known bugs that will bite you down the line worse than this.

Yes, you are not supposed to set data on player removing. That is why you are having this bug. You can just repeatedly set their data on an interval to achieve roughly the same effect.

Yes, you are not supposed to set data on player removing. That is why you are having this bug. You can just repeatedly set their data on an interval to achieve roughly the same effect.

Sounds pretty redundant imo, in my case a few 10’s or 100’s lost isn’t much of a big deal, but in a game of records that’s just a bad practice.
It’s super weird that this great module has such a wacky little limitation, but I guess it won’t be changing anytime soon, in which case I’ll drop a few questions.

Say, if I used :Set() or :Increment() etc. with DataStore2 really quickly, above the regular DataStore limitations of “60 + numPlayers × 10”, would DataStore2 also error? I know DataStore2 uses DataStore, but from what I’ve read so far it seems like it’s doing some smart caching business which eliminates the problem of this limitation. Could be very wrong, that’s why I’m asking.

And if so, does :Get() return the cached value, or does it actually try to retrieve the last saved value? Because if :Set() could be used more frequently than :Get(), the two would be “out of sync” and so :Get() could get a slightly older data.

Say, about that GitHub version… the latest version seems to be v1.3.0, which has “If data can’t save when a player leaves, it’ll no longer halt Studio.” written in the changes list. My version already doesn’t halt if it can’t save on player leaving (which at first caused me to be extremely confused for a good 30 minutes), would that mean I am using the latest version?

That should be it, thanks in advance.

1 Like

DataStore2 is just regular data stores under the hood but provides more features while still using regular data stores. So you still have to abide by the data store limits

That’s kinda weird, I’ve used :Increment() a lot more than the “60 + numPlayers × 10” limitation and it didn’t seem to stop working or malfunction?

Because

All you are doing is updating the cache.

I’ve searched on the forums and I don’t understand any single one of them. I’ve tried using “Set(ToolName, false)” but it doesn’t work. I’m very confused.

The docs exist.

https://kampfkarren.github.io/Roblox/api/

for once i actually thank you but i still don’t know what to use

Hey wait a second.
Wouldn’t that make your other statement incorrect?

Or are you referring to literally doing :Save() when you said that?

Also if I just read your quoted message correctly, :Increment() is doing :Get() and :Set() on a cache, which would mean I can :Get() and :Set() as much as I do :Increment() since it’s all working with the cache?

Which would pretty much eliminate the “limitations” since it saves everything in a super safe when actually saving the cache as data?

:Set only updates the cache it doesn’t actually save it to the data store. :Save and .SaveAll() are the ones that actually save to the data store.

2 Likes

Ohoho, that is fantastic! I’m starting to understand why DataStore2 doesn’t have data losses haha.
Thank you for all your help, now I understand the workings much more clearly.

Do you need to write:

DataStore2.Combine()

right after you require the DataStore2 module, even if another script does it?

Also, can you please link the thread where you said it?

Saving tables is no different than saving a string or number.

1 Like

It’s not. DataStore2 is designed for you to make repeated :Set() calls. DataStore2 only saves when the player leaves or :Save() is explicitly called. Just using :Set() will not trigger a save.

:Get() also only ever runs a data store call on the first try. Once it gets a value, it caches it.

2 Likes

Searching my username alongside tables should net you it. The answer is the same exact way you save everything else.