How to fix Data wipe issues?

Hello! I’ve been experiencing data wiping issue in my game. Luckily, it’s not out yet but this poses a serious threat, sometimes it loads but, sometimes my values are 0 which causes the player to restart again. Can somebody take a look in my code? Please tell me if this is the wrong category cause it works but it wipes my data sometimes.

local ds = game:GetService("DataStoreService")
local MoneyData = ds:GetDataStore("Money Store")
local WinData = ds:GetDataStore("Wins Store")
local MPData = ds:GetDataStore("MP Store")
local rs = game:GetService("ReplicatedStorage")
local player = game.Players


player.PlayerAdded:Connect(function(plr)
	local MoneyKey = ""..plr.Name.."- Money"
	local WinKey = ""..plr.Name.."- Wins"
	local MPKey = ""..plr.Name.."- Money MP"
	
	
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local Money = Instance.new("NumberValue")
	Money.Name = "💰 Money"
	Money.Value = 0
	Money.Parent = leaderstats
	
	local Wins = Instance.new("NumberValue")
	Wins.Name = "🏆 Wins"
	Wins.Value = 0
	Wins.Parent = leaderstats
	
	-- [Configs] --
	local MoneyConfig = Instance.new("Configuration")
	MoneyConfig.Name = "MoneyConfig"
	MoneyConfig.Parent = plr
	
	local MP = Instance.new("NumberValue")
	MP.Name = "MoneyMP"
	MP.Value = 1
	MP.Parent = MoneyConfig
	
	local Data_Money
	local Data_Wins
	local Data_MP
	
	local success, errormsg = pcall(function()
		Data_Money = MoneyData:GetAsync(MoneyKey)
		Data_Wins = WinData:GetAsync(WinKey)
		Data_MP = MPData:GetAsync(MPKey)
	end)
	if success then
		
	plr.leaderstats["💰 Money"].Value = Data_Money
	plr.leaderstats["🏆 Wins"].Value = Data_Wins
	plr.MoneyConfig.MoneyMP.Value = Data_MP
	
		print("Success")
	end
	
	if errormsg  then
		warn(errormsg)
		
	end
	
	wait(3)
	MP.Value = .2 * plr.leaderstats["🏆 Wins"].Value
	print("MP UPDATED")
end)

player.PlayerRemoving:Connect(function(plr)
	local MoneyKey = ""..plr.Name.."- Money"
	local WinKey = ""..plr.Name.."- Wins"
	local MPKey = ""..plr.Name.."- Money MP"
	
	local success, errormsg = pcall(function()
		MoneyData:SetAsync(MoneyKey, plr.leaderstats["💰 Money"].Value)
		WinData:SetAsync(WinKey, plr.leaderstats["🏆 Wins"].Value)
		MPData:SetAsync(MPKey, plr.MoneyConfig.MoneyMP.Value)
		print(plr.Name.." Has Left - Data Saved; Money: "..plr.leaderstats["💰 Money"].Value..", MP,"..plr.MoneyConfig.MoneyMP.Value.."and Wins: "..plr.leaderstats["🏆 Wins"].Value.."! UserID: "..plr.UserId)
	end)
	
end)

game:BindToClose(function(p)
	for i, playr in pairs(player:GetPlayers()) do
		local ID = "_Player"..playr.UserId
		
		local success, errormsg = pcall(function()
			MoneyData:SetAsync(ID, p.leaderstats["💰 Money"].Value)
			WinData:SetAsync(ID, p.leaderstats["🏆 Wins"].Value)
			MPData:SetAsync(ID, p.MoneyConfig.MoneyMP.Value)
			print(playr.Name.." Has Left - Data Saved; Money, MP, and Wins!  UserID: "..playr.UserId)
		end)
	end
	
end)
1 Like

if errormsg then
warn(errormsg)

Is there anything outputted from this?


Also,

local MoneyKey = ""..plr.Name.."- Money"

Use plr.UserId instead. plr.Name can be changed by the player and hence would cause them to lose their save.

Just a suggestion but using tables to store your data would probably be better

{
  Wins = 0,
  Money = 0,
  MoneyMp = 0,
}

Anyways for the data loss First of all I would use userid instead of username. Do these data losses occur in studio or in game? You can comfirm this by seeing if the print runs. Sometimes data doesn’t save in Roblox Studio 100% of the time but does in game. I’m assuming theres no error as you didnt provide one from the pcall.

I just noticed that the key you save is different from the one you load which is probably the problem you can try this code

local function saveData(plr)
    local MoneyKey = ""..plr.UserId.."- Money"
    local WinKey = ""..plr.UserId.."- Wins"
    local MPKey = ""..plr.UserId.."- Money MP"

    local success, errormsg = pcall(function()
        MoneyData:UpdateAsync(MoneyKey, , function ()
            return plr.leaderstats["💰 Money"].Value
        end)
        WinData:UpdateAsync(WinKey, function ()
            return plr.leaderstats["🏆 Wins"].Value
        end)
        MPData:UpdateAsync(MPKey, function ()
            return plr.MoneyConfig.MoneyMP.Value
        end)
        print(plr.Name.." Has Left - Data Saved; Money: "..plr.leaderstats["💰 Money"].Value..", MP,"..plr.MoneyConfig.MoneyMP.Value.."and Wins: "..plr.leaderstats["🏆 Wins"].Value.."! UserID: "..plr.UserId)
    end)
end



player.PlayerRemoving:Connect(function(plr)
	saveData(plr)
end)

game:BindToClose(function(p)
	for i, playr in pairs(player:GetPlayers()) do
		task.spawn(saveData, playr)
	end
end)

Both. It’s not the problem that they don’t save, the problem is my values is = 0 once I join back the game. This occurs rarely though.

No

I’ll change it to plr.UserId thanks!

1 Like