I have a Data script that holds the players levels, exp and money (and a lot more, stats etc..) and the saving worked perfectly fine. But I've had it twice now when the data randomly resets to the old values.
Here is my script:
local DataStore = game:GetService("DataStoreService") local Data = DataStore:GetDataStore("ManageData") local prefix = 'user_' game.Players.PlayerAdded:Connect(function(Plr) local stats = Instance.new("Folder", Plr) stats.Name = "Data" --- Level System local Levels = Instance.new("IntValue", stats) Levels.Name = "Levels" local Exp = Instance.new("IntValue", stats) Exp.Name = "Exp" local ExpNeed = Instance.new("IntValue", stats) ExpNeed.Name = "ExpNeed" --- Money System local Zeni = Instance.new("IntValue", stats) Zeni.Name = "Zeni" --- Stats Text local HealthP = Instance.new("IntValue", stats) HealthP.Name = "HealthPoints" local MeleeP = Instance.new("IntValue", stats) MeleeP.Name = "MeleePoints" local SpeedP = Instance.new("IntValue", stats) SpeedP.Name = "SpeedPoints" local KiMaxP = Instance.new("IntValue", stats) KiMaxP.Name = "KiMaxPoints" --- Stats System local Points = Instance.new("IntValue", stats) Points.Name = "Points" local MaxHealth = Instance.new("IntValue", stats) MaxHealth.Name = "Health Max" local Melee = Instance.new("IntValue", stats) Melee.Name = "Melee Damage" local Speed = Instance.new("NumberValue", stats) Speed.Name = "Speed" local KiMax = Instance.new("IntValue", stats) KiMax.Name = "Ki Max" -- KI DAMAGE -- local KiDamage = Instance.new("IntValue", stats) KiDamage.Name = "Ki Damage" -- KI CONTROL -- local KiControl = Instance.new("IntValue", stats) KiControl.Name = "Ki Control" -- HEALING POWER -- local HPower = Instance.new("IntValue", stats) HPower.Name = "Healing Power" -- POWER LVLS -- local powerLvl = Instance.new("IntValue", stats) powerLvl.Name = "PowerLevel" -- STAMINA -- local Stam = Instance.new("IntValue", stats) Stam.Name = "Stamina" -- START VALUES -- Levels.Value = 1 Exp.Value = 0 ExpNeed.Value = 100 Zeni.Value = 200 HealthP.Value = 1 MeleeP.Value = 1 SpeedP.Value = 1 KiMaxP.Value = 1 Points.Value = 0 MaxHealth.Value = 100 Melee.Value = 5 Speed.Value = 16 KiMax.Value = 100 KiDamage.Value = 5 KiControl.Value = 5 HPower.Value = 5 powerLvl.Value = 5 Stam.Value = 100 -- SET DATA -- local plrData = Data:GetAsync(prefix .. tostring(Plr.UserId)) if plrData then Levels.Value = plrData[1] or 1 Exp.Value = plrData[2] or 0 ExpNeed.Value = plrData[3] or 100 Zeni.Value = plrData[4] or 200 HealthP.Value = plrData[5] or 1 MeleeP.Value = plrData[6] or 1 SpeedP.Value = plrData[7] or 1 KiMaxP.Value = plrData[8] or 1 Points.Value = plrData[9] or 0 MaxHealth.Value = plrData[10] or 100 Melee.Value = plrData[11] or 5 Speed.Value = plrData[12] or 16 KiMax.Value = plrData[13] or 100 KiDamage.Value = plrData[14] or 5 KiControl.Value = plrData[15] or 5 HPower.Value = plrData[16] or 5 powerLvl.Value = plrData[17] or 5 Stam.Value = plrData[18] or 100 else Data:SetAsync(prefix .. tostring(Plr.UserId), { Plr.Data.Levels.Value, Plr.Data.Exp.Value, Plr.Data.ExpNeed.Value, Plr.Data.Zeni.Value, Plr.Data.HealthPoints.Value, Plr.Data.MeleePoints.Value, Plr.Data.SpeedPoints.Value, Plr.Data.KiMaxPoints.Value, Plr.Data.Points.Value, Plr.Data["Health Max"].Value, Plr.Data["Melee Damage"].Value, Plr.Data.Speed.Value, Plr.Data["Ki Max"].Value, Plr.Data["Ki Damage"].Value, Plr.Data["Ki Control"].Value, Plr.Data["Healing Power"].Value, Plr.Data.PowerLevel.Value, Plr.Data.Stamina.Value }) end -- SAVE STATS ON LEAVING -- game.Players.PlayerRemoving:Connect(function(Plr) Data:SetAsync(prefix .. tostring(Plr.UserId), { Plr.Data.Levels.Value, Plr.Data.Exp.Value, Plr.Data.ExpNeed.Value, Plr.Data.Zeni.Value, Plr.Data.HealthPoints.Value, Plr.Data.MeleePoints.Value, Plr.Data.SpeedPoints.Value, Plr.Data.KiMaxPoints.Value, Plr.Data.Points.Value, Plr.Data["Health Max"].Value, Plr.Data["Melee Damage"].Value, Plr.Data.Speed.Value, Plr.Data["Ki Max"].Value, Plr.Data["Ki Damage"].Value, Plr.Data["Ki Control"].Value, Plr.Data["Healing Power"].Value, Plr.Data.PowerLevel.Value, Plr.Data.Stamina.Value }) end)
Is there anything wrong with the script? Please let me know.
It might be bc you didnt pcall your GetAsync
and SetAsync
since these are network calls they can fail and can cause unpredicted behaviour