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)

5 comments
The keys are the same. I tested it by checking the values of the Data folder in the player when the player removing command is fired. I noticed it doesn't actually register anything that has changed since the player has been in the game, however, for some reason, if you check it manually in explorer, it does.
Yes. That's because you're changing it locally. As I previously stated, you cannot change values locally. The server simply won't accept it, It's part of FilteringEnabled. You will need to change your values with a server-side script, then the server will see your new values. Most things aren't replicated from the client to server automatically.
Yeah, filtering enabled prevent's clients from saving or changing server data in an attempt to decrease exploits. It also helps with other methods of scripting, and prevents players from messing with the server which then replicates to everyone else.
This script is a ServerScript, I'm confused as to what you all mean?
Oh, right yeah, I feel like an idiot now, thanks a lot.