Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is this DataStore only saving what was there when the player joined?

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)

1 answer

✓ Accepted answer

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.

5 comments

0

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.

0

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.

0

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.

0

This script is a ServerScript, I'm confused as to what you all mean?

0

Oh, right yeah, I feel like an idiot now, thanks a lot.

This discussion is preserved as it was. New answers, comments, and votes are closed.