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

How would I get data from a specific player in the command bar?

1 Like

I didnt really understand the :OnUpdate() thing.

if you modify modify your datastore it will return a function with the value after the modification (modifications: Set (), Increment ())

How should I do :Combine()?

Is it like :GetDataStore(something)?

Example:

local DataStore2 = (Your_Module)

DataStore2.Combine("GameData_V1","Power") --MasterKey,Save_Key

local PowerSave = DataStore2("Power",player)

print(PowerSave:Get(0))

use Combine with all your keys to avoid problems

1 Like

It should, yes.

There’s not currently an easy way to do this without doing something like Does CrazyMan's DataStore Editor work for DataStore2? - #6 by MFCmaster1234.

1 Like

Why is this happening, I marked where I was saving the table to the data store, is that expected to happen with tables like this? How could prevent this error from happening

That means whatever you are saving is > 260k characters. What are you trying to save? lol

A table for my wand edit thingy, basically 6 strings in a table

Looks like after encoding this table is like 450k chars long for some reason, any help would be appreciated

Ok so I just made 2 tables instead of one and it worked

What If I want to save a something like global data for a game, like a ban list, for example.

Is that possible, currently?

Hello Kampfkarren,

thx for DataStore2!
I’ve been working on my game with it for months. Now I had a public test session today and after that I put my game back to private and went directly offline. Now I wanted to make small changes, but I get the error “20:52:20.441 - HTTP 500 (Internal Server Error)” every time. Often times the exception is thrown like in the picture (but not every time). Does the error have anything to do with your DataStore2? How can I fix it? I would like to release in a few days.
httperrordatastore
Greetings from Germany

edit: The bug is fixed. I don’t think it had anything to do with DataStore2 but I’ll leave it like this if others have the same bug! And researching for it in the future :slight_smile:
I set the game back to “Public” in Studio, joined the game using the normal Roblox client, left the game again and then set the game to “Private” in Studio. Now there are no more errors.

edit2: maybe it was a temp roblox failure. Quenty (well know dev) got this error also today https://twitter.com/Quenty/status/1283108773821743104/photo/1

Hi! I was making my data store for player customization and I’m at a road block.

ReplicatedStorage.PlayerDataStoreEvents.SetBiometrics.OnServerEvent:Connect(function(player)
	
	local BiometricStore = DataStore2("PlayerBiometrics", player)
	local PlayerBiometrics = BiometricStore:Get()

	if not PlayerBiometrics then
		local MinimumHeight = 0.95
		local HeightRange = math.random(1, 12)
		local HeightIncrement = 0.017
		local PlayerHeight = MinimumHeight + HeightIncrement * HeightRange
		table.insert(BiometricStore, PlayerHeight)
		
		local MinimumWeight = 0.88
		local WeightIncrement = 0.095
		local WeightRange = math.random(1,12)
		
		if PlayerHeight > 0.1 then 
			WeightRange = math.random(6, 12)
		end
		
		if PlayerHeight < 0.1 then 
			WeightRange = math.random(1, 6)
		end
		
		local PlayerWeight = MinimumWeight + WeightIncrement * WeightRange
		table.insert(BiometricStore, PlayerWeight)
		
		local PlayerHeightChanger = player.Humanoid.BodyHeightScale
		local PlayerWeightChanger = player.Humanoid.BodyWidthScale
		local PlayerDepthChanger = player.Humanoid.BodyDepthScale
		
		PlayerHeightChanger = PlayerHeight
		PlayerWeightChanger = PlayerWeight
		PlayerDepthChanger = PlayerWeight
		
		BiometricStore:Set(PlayerBiometrics)
	else
		local PlayerHeightChanger = player.Humanoid.BodyHeightScale
		local PlayerWeightChanger = player.Humanoid.BodyWidthScale
		local PlayerDepthChanger = player.Humanoid.BodyDepthScale
		
		local PlayerHeight = PlayerBiometrics:Get(PlayerHeight)
		local PlayerWeight = PlayerBiometrics:Get(PlayerWeight)
		PlayerHeightChanger = PlayerHeight
		PlayerWeightChanger = PlayerWeight
		PlayerDepthChanger = PlayerWeight	
	end
end)

this is a snippit of code that basiclly randomizes player height/weight. As you can see at the top it has a check to see if theres player biometrics there (I’m not 100% sure if thats correct either). Then theres a else statement if there is data, this is where my issue comes In the “PlayerBiometrics:Get(PlayerHeight)” line, I get a unkown global underlined in red for “PlayerHeight” and “PlayerWeight”. Firstly I want to make sure that this is the correct way to get a value in the datastore, and if it is then could I just put a “local PlayerHeight” line just to keep it from erroring? Thanks.

Because you specify the variable as a parameter in which you write. But the variable in the parameter does not exist at the moment :get is executed, because it is not created until the right expression is executed by the equals sign. The :get method expects a default value as the first parameter, which is returned if the player in the DataStore does not yet have a value. For example, the first parameter could be 0.95. Your script would look like this:

local PlayerHeight = PlayerBiometrics:Get(0.95)
local PlayerWeight = PlayerBiometrics:Get(0.88)

Documentation: API - DataStore2

Yeah but its a randomized height, if I left it like that wouldn’t that just make it minimum height and minimum weight? At the top it creates a height and inserts it into the PlayerBiometrics datastore table, I want to get the height/weight from that table

Good day!
So, I’ve read that you are not supposed to use DataStore2’s :Set() on PlayerRemoving.
However, I need that.
I need to save a data when the player leaves.
See, normally this save would trigger when the player dies in some way, but since my game currently has no players, they can’t die, and so that specific few data cannot be saved.
Now before anyone tells me it’s really dumb to save data when a player dies, here’s why I’m doing that specifically.
See, there is a variable that when goes to 0 or below (which happens when the player dies), triggers a function, which then checks the “biggest size” and “longest time alive” values, compares them to the ones in the datastore, checking if the new values are bigger, and then if that is the case, it saves them.
These two non-datastore variables are displayed and updated in real time, 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(). It would be infinitely more performance friendly if I could also do the same checks only when the player leaves.

(Edit note: Biggest size is also used in a monthly leaderboard, so it is crucial for it to save so other players can see that people actually played the game and such.)

So, is there any possible way to save data with DataStore2 on PlayerRemoving?
Thanks in advance.

2 Likes

DataStore2 automatically saves the player’s data when the player leaves the game :slightly_smiling_face:

1 Like

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