Random data loss

Hello, I’m having issues atm, for some reason people that play my game would lose their data randomly and I have no clue why its happening. It would save some people’s data then some rare individuals would lose their data randomly. Can anyone please help?

The script

local DS = game:GetService(“DataStoreService”):GetDataStore(“PlayerData”)

game.Players.PlayerAdded:Connect(function(Player)
wait()

local PLRKEY = "id"..Player.userId

local DataFolder = Instance.new("Folder")
DataFolder.Name = "PlayerData"
DataFolder.Parent = Player

local CurrentLevel = Instance.new("IntValue")
CurrentLevel.Name = "CurrentLevel"
CurrentLevel.Parent = DataFolder

local CurrentDifficulty = Instance.new("IntValue")
CurrentDifficulty.Name = "CurrentDifficulty"
CurrentDifficulty.Parent = DataFolder

local Level1Difficulty = Instance.new("IntValue")
Level1Difficulty.Name = "Level1Difficulty"
Level1Difficulty.Parent = DataFolder

local Level2Difficulty = Instance.new("IntValue")
Level2Difficulty.Name = "Level2Difficulty"
Level2Difficulty.Parent = DataFolder

local Level3Difficulty = Instance.new("IntValue")
Level3Difficulty.Name = "Level3Difficulty"
Level3Difficulty.Parent = DataFolder

local Level4Difficulty = Instance.new("IntValue")
Level4Difficulty.Name = "Level4Difficulty"
Level4Difficulty.Parent = DataFolder

local Level5Difficulty = Instance.new("IntValue")
Level5Difficulty.Name = "Level5Difficulty"
Level5Difficulty.Parent = DataFolder

local Level6Difficulty = Instance.new("IntValue")
Level6Difficulty.Name = "Level6Difficulty"
Level6Difficulty.Parent = DataFolder

local Level7Difficulty = Instance.new("IntValue")
Level7Difficulty.Name = "Level7Difficulty"
Level7Difficulty.Parent = DataFolder

local Level8Difficulty = Instance.new("IntValue")
Level8Difficulty.Name = "Level8Difficulty"
Level8Difficulty.Parent = DataFolder

local Level1Highscore = Instance.new("IntValue")
Level1Highscore.Name = "Level1Highscore"
Level1Highscore.Parent = DataFolder

local Level2Highscore = Instance.new("IntValue")
Level2Highscore.Name = "Level2Highscore"
Level2Highscore.Parent = DataFolder

local Level3Highscore = Instance.new("IntValue")
Level3Highscore.Name = "Level3Highscore"
Level3Highscore.Parent = DataFolder

local Level4Highscore = Instance.new("IntValue")
Level4Highscore.Name = "Level4Highscore"
Level4Highscore.Parent = DataFolder

local Level5Highscore = Instance.new("IntValue")
Level5Highscore.Name = "Level5Highscore"
Level5Highscore.Parent = DataFolder

local Level6Highscore = Instance.new("IntValue")
Level6Highscore.Name = "Level6Highscore"
Level6Highscore.Parent = DataFolder

local Level7Highscore = Instance.new("IntValue")
Level7Highscore.Name = "Level7Highscore"
Level7Highscore.Parent = DataFolder

local Level8Highscore = Instance.new("IntValue")
Level8Highscore.Name = "Level8Highscore"
Level8Highscore.Parent = DataFolder

local Highscore = Instance.new("IntValue")
Highscore.Name = "Highscore"
Highscore.Parent = DataFolder

local Chaos = Instance.new("IntValue")
Chaos.Name = "Chaos"
Chaos.Parent = DataFolder

local Donation = Instance.new("IntValue")
Donation.Name = "Donation"
Donation.Parent = DataFolder

-- NONE SAVED

local Score = Instance.new("IntValue")
Score.Name = "Score"
Score.Parent = DataFolder

wait(0.001)

local SAVE1 = Player.PlayerData.CurrentLevel
local SAVE2 = Player.PlayerData.CurrentDifficulty
local SAVE3 = Player.PlayerData.Level1Difficulty
local SAVE4 = Player.PlayerData.Level2Difficulty
local SAVE5 = Player.PlayerData.Level3Difficulty
local SAVE6 = Player.PlayerData.Level4Difficulty
local SAVE7 = Player.PlayerData.Level5Difficulty
local SAVE8 = Player.PlayerData.Level6Difficulty
local SAVE9 = Player.PlayerData.Level7Difficulty
local SAVE10 = Player.PlayerData.Level8Difficulty
local SAVE11 = Player.PlayerData.Highscore
local SAVE12 = Player.PlayerData.Chaos
local SAVE13 = Player.PlayerData.Level1Highscore
local SAVE14 = Player.PlayerData.Level2Highscore
local SAVE15 = Player.PlayerData.Level3Highscore
local SAVE16 = Player.PlayerData.Level4Highscore
local SAVE17 = Player.PlayerData.Level5Highscore
local SAVE18 = Player.PlayerData.Level6Highscore
local SAVE19 = Player.PlayerData.Level7Highscore
local SAVE20 = Player.PlayerData.Level8Highscore
local SAVE21 = Player.PlayerData.Donation

local GetSaved = DS:GetAsync(PLRKEY)

if GetSaved then
	SAVE1.Value = GetSaved[1]
	SAVE2.Value = GetSaved[2]
	SAVE3.Value = GetSaved[3]
	SAVE4.Value = GetSaved[4]
	SAVE5.Value = GetSaved[5]
	SAVE6.Value = GetSaved[6]
	SAVE7.Value = GetSaved[7]
	SAVE8.Value = GetSaved[8]
	SAVE9.Value = GetSaved[9]
	SAVE10.Value = GetSaved[10]
	SAVE11.Value = GetSaved[11]
	SAVE12.Value = GetSaved[12]
	SAVE13.Value = GetSaved[13]
	SAVE14.Value = GetSaved[14]
	SAVE15.Value = GetSaved[15]
	SAVE16.Value = GetSaved[16]
	SAVE17.Value = GetSaved[17]
	SAVE18.Value = GetSaved[18]
	SAVE19.Value = GetSaved[19]
	SAVE20.Value = GetSaved[20]
	SAVE21.Value = GetSaved[21]
else
	local NumbersForSaving = {SAVE1.Value, SAVE2.Value, SAVE3.Value, SAVE4.Value, 
		SAVE5.Value, SAVE6.Value, SAVE7.Value, SAVE8.Value, SAVE9.Value, SAVE10.Value,
		SAVE11.Value, SAVE12.Value, SAVE13.Value, SAVE14.Value, SAVE15.Value, SAVE16.Value,
		SAVE17.Value, SAVE18.Value, SAVE19.Value, SAVE20.Value, SAVE21.Value}
	
	DS:GetAsync(PLRKEY, NumbersForSaving)
end

CurrentDifficulty.Value = 1

repeat wait() until Player.Character

wait(1)

if Chaos.Value >= 1 then
	wait(12)
	Chaos.Value = 0
end

end)

game.Players.PlayerRemoving:Connect(function(Player)

local SAVE1 = Player.PlayerData.CurrentLevel
local SAVE2 = Player.PlayerData.CurrentDifficulty
local SAVE3 = Player.PlayerData.Level1Difficulty
local SAVE4 = Player.PlayerData.Level2Difficulty
local SAVE5 = Player.PlayerData.Level3Difficulty
local SAVE6 = Player.PlayerData.Level4Difficulty
local SAVE7 = Player.PlayerData.Level5Difficulty
local SAVE8 = Player.PlayerData.Level6Difficulty
local SAVE9 = Player.PlayerData.Level7Difficulty
local SAVE10 = Player.PlayerData.Level8Difficulty
local SAVE11 = Player.PlayerData.Highscore
local SAVE12 = Player.PlayerData.Chaos
local SAVE13 = Player.PlayerData.Level1Highscore
local SAVE14 = Player.PlayerData.Level2Highscore
local SAVE15 = Player.PlayerData.Level3Highscore
local SAVE16 = Player.PlayerData.Level4Highscore
local SAVE17 = Player.PlayerData.Level5Highscore
local SAVE18 = Player.PlayerData.Level6Highscore
local SAVE19 = Player.PlayerData.Level7Highscore
local SAVE20 = Player.PlayerData.Level8Highscore
local SAVE21 = Player.PlayerData.Donation

DS:SetAsync("id"..Player.userId, {SAVE1.Value, SAVE2.Value, SAVE3.Value, SAVE4.Value, 
	SAVE5.Value, SAVE6.Value, SAVE7.Value, SAVE8.Value, SAVE9.Value, SAVE10.Value,
	SAVE11.Value, SAVE12.Value, SAVE13.Value, SAVE14.Value, SAVE15.Value, SAVE16.Value,
	SAVE17.Value, SAVE18.Value, SAVE19.Value, SAVE20.Value, SAVE21.Value})

end)

local ChangeDifficultyR = game.ReplicatedStorage.RemoteData.LevelRemotes.ChangeDifficulty

ChangeDifficultyR.OnServerEvent:Connect(function(Player, Difficulty)
local PlayerData = Player:FindFirstChild(“PlayerData”)

local CurrentDifficulty = PlayerData:FindFirstChild("CurrentDifficulty")

CurrentDifficulty.Value = Difficulty

end)

i’m not entirely sure what your issue is but there are some things i can point out.

wrap your SetAsyncs and GetAsyncs in a pcall incase it errors, and make it so the player’s data can’t save if there is an error loading it. If there was an error loading, and it saved when they left, their data would be overwritten.

In the playeradded section, in your else statement, i’m pretty sure you meant to have SetAsync instead of GetAsync.

The issue is the data does not save very well also I have never use pcall before so Idk what you mean by wrap SetAsyncs and GetAsyncs in a pcall.

you basically do like

local succ, err = pcall(function()
    DS:SetAsync(PLRKEY, NumbersForSaving)
end)

if succ then
-- whatever
else
   warn(err)
end

Use UpdateAsync instead because SetAsync if the setasync fails then the data is gone instead with UpdateAsync the data is being updated instead of being set

-- EXAMPLE
local tbltosave = {SAVE1.Value, SAVE2.Value, SAVE3.Value, SAVE4.Value, 
	SAVE5.Value, SAVE6.Value, SAVE7.Value, SAVE8.Value, SAVE9.Value, SAVE10.Value,
	SAVE11.Value, SAVE12.Value, SAVE13.Value, SAVE14.Value, SAVE15.Value, SAVE16.Value,
	SAVE17.Value, SAVE18.Value, SAVE19.Value, SAVE20.Value, SAVE21.Value}
DS:UpdateAsync("id"..Player.userId,function(olderdata)
return tbltosave or olderdata
end)
1 Like

Where do I place that at in the script?

Ill try that out and see if it works

1 Like

Sadly, nothing is being saved atm

You have a warning or a error on your devconsole?

Nothing on the devconsole, no warnings or errors

I recommend to not use the default Data store service for bigger games with higher counts of people, It just has data loss which can be annoying, like those limits. It’s not a great service, for data saving and there is a lot of better ways to save data. I use the profile service which is made by devs, That have spent a long, long time for saving data. This is what I recommend, and connecting it so you slowly change from one data set to the other. Below I have a video (made by MonzterDEV) that’s made on the profile service & it explains it well. Profile Service Video

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