What do you want to achieve?
I recently converted my game over to DataStore2. I want to use it as a regular DataStore and just save values.
What is the issue?
The issue is that I have made a value called “Cash” and it saves normally, I have a Part with a ClickDetector and when I click it, it increments my cash. It works perfectly and prints fine. However, when I shutdown the Cash value does not save and it gets reverted back.
What solutions have you tried so far?
I saw other forum posts on DataStore2 and I saw a solution that said to make a BindToClose function and do DataStore2:Save() but when I tried doing this it gives me the error “attempt to call a nil value” and it did not work. I also have heard that DataStore2 saves data automatically on shutdowns but I tested this and it still reverts back for me.
Any help is appreciated.
local ServerScriptService = game:GetService("ServerScriptService")
local Datastore = require(ServerScriptService.DataStore2)
local Players = game:GetService("Players")
Datastore.Combine("Cash")
Players.PlayerAdded:Connect(function(Player)
local Cash = Datastore("Cash", Player)
Cash:Get(100)
local function CashUpdated()
print(Cash:Get(100))
end
Cash:OnUpdate(CashUpdated)
end)
game:BindToClose(function()
Datastore:Save() -- This errors
wait(3)
end)
DataStore2 automatically binds to close so you don’t need to do it yourself.
I’ve also improved your code:
local ServerScriptService = game:GetService("ServerScriptService")
local Datastore = require(ServerScriptService.DataStore2)
local Players = game:GetService("Players")
Datastore.Combine("DATA", "Cash")
Players.PlayerAdded:Connect(function(Player)
local Cash = Datastore("Cash", Player)
Cash:Get(100)
local function CashUpdated(UpdatedCash)
print(UpdatedCash)
end
Cash:OnUpdate(CashUpdated)
end)
For Datastore.Combine(), you use use a master key first, then put your normal keys. You can make this “DATA”.
Instead of using Cash:Get(), just use the parameter. It returns the updated value.
For me it does not save on shutdowns, also thanks for the updated code! But I am still having the shutdown issues. I use the normal DataStore2 from the website. I am not sure why this is happening.
The free model has the bug where it doesn’t save on shutdown. The release on GitHub, which is the only one that’s recommended, does not have this bug. Are you using the release from GitHub?