Hi,
I've made it so my DataStore saves when a player leaves the game. However, it wont save properly. It is only saving what was already saved when the player entered the game and nothing that happens to the values during the game will be saved when the player is leaving the game, like it should.
For example,
The player joins with 120 cash.
The player gains 20 cash.
The player now has 140 cash.
The player leaves.
DataStore saves 120 cash.
I have no idea why this is happening?
This is the saving script.
local DataStore=game:GetService("DataStoreService") local CashStore=DataStore:GetDataStore("Currency") local WarrantStore=DataStore:GetDataStore("Warrant") local CBOStore=DataStore:GetDataStore("CBO") game.Players.PlayerRemoving:Connect(function(plr) local Cash=plr.DataValues.Cash.Value print(Cash) local Warrant=plr.DataValues.Warrant.Value local CBO=plr.DataValues.CBO.Value local success, err=pcall(function() -- Cash Saving CashStore:SetAsync(plr.UserId, Cash) end) if not success then warn("DataStore cash save request failed for " .. plr.UserId) warn(err) end -- Cash Error Handling local success, err=pcall(function() -- Warrant Saving WarrantStore:SetAsync(plr.UserId, Warrant) end) if not success then warn("DataStore warrant save request failed for " .. plr.UserId) warn(err) end -- Warrant Error Handling local success, err=pcall(function() -- CBO Saving CBOStore:SetAsync(plr.UserId, CBO) end) if not success then warn("DataStore CBO save request failed for " .. plr.UserId) warn(err) end -- CBO Error Saving end)
That's fine. You will need to make sure that whatever code changes the Values for the datastores to grab is a server-side script. If you're changing values in the studio client, it won't save because you're changing it locally. Load a command such as Player.Zeluxis.DataValues.Cash.Value = 140, then try rejoining.
If that doesn't work, you're going to have to show your GetAsync code as well. Make sure the GetAsync's first field is the correct player's key that's used in your SetAsync.