DataStore saving error

I’m making a DataStore system (V1, not 2), but it falsely saves the player’s data when they leave.
I click a part that gives me 1 money / click, which is how I easily increase my stats.
image
“Set-Up…” is announced when the player joins
“Saved…” is announced when the player leaves (I used an alt account to check this)

And the leaderstats returned a 0 when I rejoined.
image

This is the code I’m using to save data

function playerHandler.SaveData(player)
	local playerUserId = player.UserId
	
	local success, err = pcall(function()
		PLAYER_DATA:SetAsync(playerUserId, sessionData[playerUserId])
	end)
	if success then
		print("Saved data for player "..player.Name)
	else
		error(tostring(err))
	end
end

I even added a manual save to the click part that saves every time I click, and the log even returned that my DataStore request was queued, so it must be taking notice?
I’d be grateful if anyone could help.

Show the code that loads your data please

Sorry for the late reply, I had to go to sleep.

function playerHandler.SetUpPlayerData(player)
	local playerUserID = player.UserId
	
	local data
	local success, err = pcall(function()
		data = PLAYER_DATA:GetAsync(playerUserID)
	end)
	if success and data then
		sessionData[playerUserID] = data
		
		if data then
			player.leaderstats:WaitForChild("Money").Value = data.Money
			player.leaderstats:WaitForChild("Distance").Value = data.Distance
		end
	elseif err then
		error(tostring(err))
	else
		sessionData[playerUserID] = {
			Money = player.leaderstats:WaitForChild("Money").Value,
			Distance = player.leaderstats:WaitForChild("Distance").Value
		} 
		print("Set-up data for "..player.Name)
		
	end
	
end

PLAYER_DATA is just a GetDataStore call for a store called “playerHandler”

try printing success and data before you do the if statement and after the pcall

...
	local data
	local success, err = pcall(function()
		data = PLAYER_DATA:GetAsync(playerUserID)
	end)
	if success then
		print(success)
		for i, v in pairs(data) do
			print(i, v)
		end
	end
	if success and data then
		sessionData[playerUserID] = data
...

This returns
image

Try printing sessionData[playerUserId] in the function that saves the data.


It mustn’t be able to read the data properly then?

This should work:

function playerHandler.SaveData(player)
	local playerUserId = player.UserId
	
	local success, err = pcall(function()
		PLAYER_DATA:SetAsync(playerUserId, {Money = player.leaderstats.Money.Value, Distance = player.leaderstats.Distance.Value})
	end)
	if success then
		print("Saved data for player "..player.Name)
	else
		error(tostring(err))
	end
end

Same issue here.

function playerHandler.SaveData(player)
	local playerUserId = player.UserId
	
	local success, err = pcall(function()
		PLAYER_DATA:SetAsync(playerUserId, {
			Money = player.leaderstats.Money.Value, 
			Distance = player.leaderstats.Distance.Value
		})
	end)
	if success then
		print("Saved data for player "..player.Name)
		for i, v in pairs(sessionData[playerUserId]) do
			print(i, v)
		end
	else
		error(tostring(err))
	end
end

Show the script that calls the function to save the player data, please. Do you change the player’s dictionary value when their stats change?

This is inside a part that I click to give me +1 Money, and it saves every time as well (purely for debugging purposes, once I fix this I’ll remove the part anyway).

local module = require(game.ReplicatedStorage.Universal.DataStoreSettings)

script.Parent.MouseClick:Connect(function(player)
	player.leaderstats.Money.Value = player.leaderstats.Money.Value + 1
	module.SaveData(player)
end)

I did some looking around and fixed the problem. The game wasn’t writing to the DataStore properly so I made a workaround.