Hello, 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 only the health and position of the player when they leave. Reading the documentation on datastore2 saving on player removing should still work
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)
Calling Set() only updates the value in DataStore2’s cache.
You need to call DataStore:Save() or DataStore2.SaveAll(Player) (note the latter uses the dot operator) in order to push your changes to DataStoreService.
Do I need to do this when I use set at all times? Never used DataStore:Save() or DataStore2.SaveAll(Player), using :Save()/SaveAll hits datastore limits?
Never used DataStore:Save() or DataStore2.SaveAll(Player)
No offense, but what in god’s name have you been doing with DS2 up until this point? You need to use either of these two methods to actually save whatever you’re trying to save on the cloud; without invoking them, you’re just storing the data locally.
At the end of the day, DataStore2 is just an interface for DataStoreService that has a cache and some data-saving tricks you can take advantage of. It still needs to make a call to DataStoreService at some point, and Save() and SaveAll() are how it knows to do this. If it made calls to DSS for you each time you invoked Set(), then you’d start getting throttled any time you tried to save a value too often, which is exactly what DataStore2 is designed to avoid.
Do I need to do this when I use set at all times?
No, you should not because that defeats the purpose of DataStore2.
The way you’re supposed to be using DataStore2, to my understanding, is:
Use getter & setter functions (Get(), Set(), etc.) to read from and write to values in the database each time you interact with them.
When you’re done accessing the data (such as when the player disconnects from the server), you should use SaveAll() to write the data to DataStoreService in one request.
In your case, since you’re writing to the database only when the player is disconnecting anyway, then technically yes, you should be using SaveAll() after you made your Set() calls, but in general you should be putting off using SaveAll() as long as you can.
I’m pretty sure datastore2 automatically saves at the end of a session, never used Save() or .SaveAll() and always had my data save, except at this particular time.
Yes! DataStore2 saves on the game event BindToClose and also the Player event “PlayerRemoving”, all you have to do is tell it what exactly to save the data as prior to them leaving.
It appears using the model version of datastore2 fixed the issue? Weird, I thought the updated version was in github.
EDIT: (The github version is the updated version) With the new version you can’t save when the player leaves at all. Also, no you don’t have to use :save() or .saveall(), datastore2 stores data in it’s own cache when :set() or increment:() is called, then uses roblox datastores when the player leaves.