Hi. I am trying to make the currency in my game save with Data Store. It currently saves with Data Persistence, but as I am creating more features throughout my game's universe, I will need it to be in Data Store.
I have tried many different things, but nothing has worked.
Load:
ds = game:GetService("DataStoreService"):GetDataStore("Money") game.Players.PlayerAdded:connect(function(player) local value = player.Money wait(1) value.Value = ds:GetAsync(player.userId) end)
Save:
ds = game:GetService("DataStoreService"):GetDataStore("Money") game.Players.PlayerRemoving:connect(function(player) local value = player.Money wait(1) ds:SetAsync(player.userId, value.Value) end)
My game is set up so when you join an IntValue is created directly inside of game.Players.LocalPlayer. Whenever you purchase something, a certain number is subtracted from that value. A LocalScript in the PlayerGui continuously checks for updates to the Value, and when it does it changes the text of a TextLabel which shows how much money you have.
I get no errors in my output when I start the game. Everything works fine except for one of those two scripts. Both of the codes are run in a Script (not LocalScript, etc.) and they are being tested in online mode. Thanks for your time!
When I setup my DataStore's usually never do
value.Value = ds:GetAsync()
Usually you should try to GetAsync() in a variable like
Data = ds:GetAsync()
then you do
value.Value = Data
I'm not thoroughly sure if this is the correct thing you need but it's something I suggest to improve. It may help within the Load and Save correctly also
:UpdateAsync in my opinion would be a better solution for your problem, since SetAsync is sort of creating the Key associated with Value. But I could be wrong as I'm not that familiar with DataStore, here is what I'd do: Load:
ds = game:GetService("DataStoreService"):GetDataStore("Money") game.Players.PlayerAdded:connect(function(player) local valuea = player.Money wait(1) valuea.Value = ds:GetAsync(player.userId) end)
Save:
ds = game:GetService("DataStoreService"):GetDataStore("Money") game.Players.PlayerRemoving:connect(function(player) local valuea = player.Money wait(1) ds:UpdateAsync(player.userId, valuea.Value) end)