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)