SetAsync() stopped saving data out of nowhere

(Accidentally posted the same topic without finishing the post, this is not a duplicate.)

It used to work no problem, until all of a sudden it stopped saving data. My XP is stuck at last saved/old value; whenever it increases, and then rejoin, it turns back to old value. I realized it when someone from my server said that their XP nor other data is not being saved anymore.

I don’t know what caused this. I didn’t change the script recently other than adding like 2 more stats, which is no different in the duplicated code except the name change (I’m not exactly sure about the time it stopped working).

Weird thing is, I put print() after playerData:SetAsync() and it prints “Saved!” when I leave the game, but in reality it does not.

Access to API Services is always enabled. The script is server-sided located in ServerScriptService.

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")

local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local hiddenstats = Instance.new("Folder")
	hiddenstats.Name = "hiddenstats"
	hiddenstats.Parent = leaderstats
	local sample = Instance.new("IntValue")
	sample.Name = "Sample"
	sample.Parent = hiddenstats
	local dsc = Instance.new("IntValue")
	dsc.Name = "Distance"
	dsc.Parent = hiddenstats
	local min = Instance.new("IntValue")
	min.Name = "Minute"
	min.Parent = hiddenstats
	local pizza = Instance.new("IntValue")
	pizza.Name = "Pizza"
	pizza.Parent = hiddenstats
	local coffee = Instance.new("IntValue")
	coffee.Name = "Coffee"
	coffee.Parent = hiddenstats
	local exp = Instance.new("IntValue")
	exp.Name = "Experience"
	exp.Parent = leaderstats
	local playerUserId = "Player_" .. player.UserId 
	local data = playerData:GetAsync(playerUserId) 
	if data then
		sample.Value = data['Sample']
		dsc.Value = data['Distance']
		exp.Value = data['Experience']
		min.Value = data['Minute']
		pizza.Value = data['Pizza']
		coffee.Value = data['Coffee']
	else
		sample.Value = 0
		dsc.Value = 0
		exp.Value = 0
		min.Value = 0
		pizza.Value = 0
		coffee.Value = 0
	end
end


local function create_table(player)
	local player_stats = {}
	for _, stat in pairs(player.leaderstats:GetDescendants()) do 
		if stat.ClassName == "IntValue" then
			player_stats[stat.Name] = stat.Value
		end
	end
	return player_stats
end

local function onPlayerExit(player)
	local player_stats = create_table(player)
	local success, err = pcall(function()
		local playerUserId = "Player_" .. player.UserId
		playerData:SetAsync(playerUserId, player_stats) --Saves player data
		print("Saved!") --It does print this in output, however no data is saved when rejoined
	end)
	if not success then
		warn('Failed while saving data!')
	end
end


game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
1 Like

If this problem is in Roblox Studio then it’s normal, in normal test Roblox Studio doesn’t always save data to prevent corrupted data. If you want to see the full save, use the thing as Team Test, usually the data is always saved there.

image

under the normal test i have the value of the “Play” button

It doesn’t save within game either, that’s the problem. The way I realized it doesn’t save anymore is by some player from my server told me about this problem.

Game:bindtoclose(function()
        Task.wait(3)
end)

This should make it so it works in roblox studio. I hope. And i suggest putting a pcall at getasync() so you knows if the data loads or not.

The data does load, that’s why the old values are still present (e.g. the game always loads my XP as “10”, and whenever I increase it (for example, to “13”) and rejoin it’s back to “10”).
As I said, it used to save & load all stats no problem, I don’t know why it occurred out of nowhere.

I realized my mistake.

The scripts that “increased” the XP count without saving were client-sided… I must’ve totally been ignored that.

I thought of that because I realized that XP gained from some other events (older ones) did actually save. Localscripts only run on client side and it could’ve not seen by the server. I’ll use remote events instead.

(It’s not the script I provided in my post that is a localscript, it’s the ones that changed the values.)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.