Data Saving Problem

For some reason in the script only Money and Wins will save. It’s set up as expected and before saving runs as expected but during saving only Money and Wins save. I’ve been trying for months to fix it but I can’t figure it out. Can anyone help?

local DataStoreService = game:GetService("DataStoreService")
local PlayerStatsDataStore = DataStoreService:GetDataStore("PlayerStats")

local unsavedPlayerData = {}

local function retrievePlayerStats(player)
	local success, savedStats = pcall(function()
		return PlayerStatsDataStore:GetAsync(tostring(player.UserId))
	end)

	if success and savedStats then
		player.Money.Value = savedStats.Money or 0
		player.leaderstats.Wins.Value = savedStats.Wins or 0
		player.Exp.Value = savedStats.Exp or 0
		player.Levels.Value = savedStats.Levels or 0
		player.Rebirth.Value = savedStats.Rebirth or 0
	else
		warn("Failed to retrieve player stats:", savedStats)
		-- Set default values if data retrieval fails
		player.Money.Value = 0
		player.leaderstats.Wins.Value = 0
		player.Exp.Value = 0
		player.Levels.Value = 0
		player.Rebirth.Value = 0
	end
end

local function savePlayerStats(player)
	local success, error = pcall(function()
		local stats = {
			Money = player.Money.Value,
			Wins = player.leaderstats.Wins.Value,
			Exp = player.Exp.Value,
			Levels = player.Levels.Value,
			Rebirth = player.Rebirth.Value,
		}

		PlayerStatsDataStore:SetAsync(tostring(player.UserId), stats)
	end)

	if not success then
		warn("Error saving player stats:", error)
		-- Handle the error as needed, e.g., retrying or notifying the player
	else
		print("Saving was successful")
	end
end

local function saveAllPlayerStats()
	for userId, stats in pairs(unsavedPlayerData) do
		local success, error = pcall(function()
			PlayerStatsDataStore:SetAsync(userId, stats)
		end)

		if not success then
			warn("Error saving player stats:", error)
			-- Handle the error as needed, e.g., retrying or notifying the player
		else
			print("Saving was successful")
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = player:FindFirstChild("leaderstats")
	if not leaderstats then
		leaderstats = Instance.new("Folder")
		leaderstats.Name = "leaderstats"
		leaderstats.Parent = player

		local moneyValue = Instance.new("IntValue")
		moneyValue.Name = "Money"
		moneyValue.Parent = player
		moneyValue.Value = 0

		local WinsValue = Instance.new("IntValue")
		WinsValue.Name = "Wins"
		WinsValue.Parent = leaderstats
		WinsValue.Value = 0

		local ExpValue = Instance.new("IntValue")
		ExpValue.Name = "Exp"
		ExpValue.Parent = player
		ExpValue.Value = 0

		local LevelsValue = Instance.new("IntValue")
		LevelsValue.Name = "Levels"
		LevelsValue.Parent = player
		LevelsValue.Value = 0

		local RebirthValue = Instance.new("IntValue")
		RebirthValue.Name = "Rebirth"
		RebirthValue.Parent = player
		RebirthValue.Value = 0
	end

	-- Make sure the values exist before trying to set their values
	local expValue = player:FindFirstChild("Exp")
	local levelsValue = player:FindFirstChild("Levels")
	local rebirthValue = player:FindFirstChild("Rebirth")

	if not expValue then
		expValue = Instance.new("IntValue")
		expValue.Name = "Exp"
		expValue.Parent = player
		expValue.Value = 0
	else
		print("EXPValue Found")
	end

	if not levelsValue then
		levelsValue = Instance.new("IntValue")
		levelsValue.Name = "Levels"
		levelsValue.Parent = player
		levelsValue.Value = 0
	else
		print("LevelsValue Found")
	end

	if not rebirthValue then
		rebirthValue = Instance.new("IntValue")
		rebirthValue.Name = "Rebirth"
		rebirthValue.Parent = player
		rebirthValue.Value = 0
	else
		print("RebirthValue Found")
	end

	retrievePlayerStats(player)

	player.CharacterRemoving:Connect(function()
		savePlayerStats(player)
	end)
end)


game.Players.PlayerRemoving:Connect(function(player)
	savePlayerStats(player)
end)

This may not be a case of broken datastore saving, instead, you may be altering Exp, Levels, and Rebirths on the Client instead of the Server. If this is the case, from your point of view (the Client), it may look like your Exp and so on, are being changed, however, because those values are being changed on the Client, they are not being replicated to the Server. Datastore saving occurs on the Server, so if there is nothing to save on the Server, then it will load nothing.

I could be completely wrong, but may you check to see if you are using Server Scripts and/or Remote Events for altering the values of Exp and so on.

You may be right since money and wins is changed on the server compared to the rest. I’ll check soon

Basically this is what sort of fixed it
local PlayerDataStore = DataStoreService:GetDataStore("PlayerStats")

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