Help with data loss

**local ds = game:GetService(“DataStoreService”)
local dsm = ds:GetDataStore(“Main”)

task.spawn(function()
game.Players.PlayerAdded:Connect(function(plr)
local nv = Instance.new(“BoolValue”, plr)
nv.Name = “Loaded”
nv.Value = false
task.spawn(function()
task.wait(5)
nv.Value = true
end)
task.wait(5)
repeat
wait()
until plr.CharacterAppearanceLoaded
local dse = ds:GetDataStore(“Main”)
local gd = dse:GetAsync(plr.UserId)
if gd then
pcall(function()
plr.data.Points.Value = gd[1]
plr.data.Characters.TSM.Value = gd[2]
plr.data.Characters.SM.Value = gd[3]
plr.data.Characters.LCM.Value = gd[4]
plr.Donated.Value = gd[5]
plr.data.Characters.TCM.Value = gd[6]
plr.data.Characters.TV.Value = gd[7]
plr.data.Characters.SCM.Value = gd[8]
plr.data.Characters.TTV.Value = gd[9]
plr.data.Characters.TTVC.Value = gd[10]
plr.data.Characters.UTSM.Value = gd[11]
plr.data.Characters.NCM.Value = gd[12]
end)
end

	task.spawn(function()
		while wait(120) do
			local stufftosave = {
				plr.data.Points.Value,
				plr.data.Characters.TSM.Value,
				plr.data.Characters.SM.Value,
				plr.data.Characters.LCM.Value,
				plr.Donated.Value,
				plr.data.Characters.TCM.Value,
				plr.data.Characters.TV.Value,
				plr.data.Characters.SCM.Value,
				plr.data.Characters.TTV.Value,
				plr.data.Characters.TTVC.Value,
				plr.data.Characters.UTSM.Value,
				plr.data.Characters.NCM.Value
			}

		if plr.Loaded.Value == true then
				local succes, erromsg = pcall(function()
					local dse = ds:GetDataStore("Main")
					dse:UpdateAsync(plr.UserId, function(oldvalue)
						if stufftosave ~= oldvalue then
							return stufftosave
						end
					end)
				end)

				if succes then
					print("saved")
				else
					warn(erromsg)
				end

		end
		end
	end)
end)

end)

task.spawn(function()
game.Players.PlayerRemoving:Connect(function(plr)
local stufftosave = {
plr.data.Points.Value,
plr.data.Characters.TSM.Value,
plr.data.Characters.SM.Value,
plr.data.Characters.LCM.Value,
plr.Donated.Value,
plr.data.Characters.TCM.Value,
plr.data.Characters.TV.Value,
plr.data.Characters.SCM.Value,
plr.data.Characters.TTV.Value,
plr.data.Characters.TTVC.Value,
plr.data.Characters.UTSM.Value,
plr.data.Characters.NCM.Value
}

	if plr.Loaded.Value == true then
		local succes, erromsg = pcall(function()
			local dse = ds:GetDataStore("Main")
			dse:UpdateAsync(plr.UserId, function(oldvalue)
				if stufftosave ~= oldvalue then
					return stufftosave
				end
			end)
		end)

		if succes then
			print("saved")
		else
			warn(erromsg)
		end

	end

end)

end)

game:BindToClose(function()
for i,v in game.Players:GetChildren() do
local stufftosave = {
v.data.Points.Value,
v.data.Characters.TSM.Value,
v.data.Characters.SM.Value,
v.data.Characters.LCM.Value,
v.Donated.Value,
v.data.Characters.TCM.Value,
v.data.Characters.TV.Value,
v.data.Characters.SCM.Value,
v.data.Characters.TTV.Value,
v.data.Characters.TTVC.Value,
v.data.Characters.UTSM.Value,
v.data.Characters.NCM.Value
}

	local succes, erromsg = pcall(function()
		local dse = ds:GetDataStore("Main")
		dse:UpdateAsync(v.UserId, function(oldvalue)
			if stufftosave ~= oldvalue then
				return stufftosave
			end
		end)
	end)

	if succes then
		print("saved")
	else
		warn(erromsg)
	end
end

end)
**

help with data loss my code is the above and it happens often as i gotta keep restoring data

bro got the best formatting ive seen in years

3 Likes

:skull: sorry I sent it from phone that’s why it sucks

But the code is from pc, idk why it happens no error no nothing

Maybe because I make the values In a diff script?

Are there any error messages or smt

You should always do data handling generally in one script

No, it saves for me and I have never lost data but some player lose data and make complaints on the discord

Does it save for you in the real game AND studios or one or the other

And i dont see anything wrong w it
I suggest using something like DataStore2 ot ProfileService

Yes it saves in the studio and in the real game

Think it might just be bad luck for the player if it works fine for you.
Datastores are like this, if it doesnt work for someone theres a big chance it doesnt work for another. But if it works for some and not others, theres a chance that the data could be corrupted or some confliction going on

maybe u should ask help once ur on ur computer so u can better format the script and know whats going on

Well said, the 43rd President of the United States

1 Like

Hey Mr… zaza…?,

Waiting arbitrarily for data to load isn’t the most reliable way. Datastore requests can take time or even fail. With this code logic your player data might appear loaded even when it isn’t.

So saving data that hasn’t actually loaded yet causes data that were not yet set to be saved. (the data wipes)

Waiting an arbitrary amount of time then just saying “this data Loaded” is a logic error. Consider making sure “gd” actually has a value before marking the player data as “Loaded”.

This is kind of saving is bad because sometimes DataStore requests can take up to 10 seconds or more, and/or sometimes they throw out an error which can cause issues if not handled correctly.

Let’s say the server rejected our DataStore request…

502: API Services rejected request with error. Error code: 0 Reason: An internal server error occurred.

When DataStores throw an error with this code you provided, Player data will never be loaded since there is no code checking if it loaded or if the request failed!

But the Loaded.Value = true will still run… You see where I’m going with this?

Player leaving will completely wipe their data since they were never loaded but appeared as loaded.

How can you fix it?

Error handling

  1. Wrap your DataStore:GetAsync() call in a pcall function. This catches any errors during the request.
  2. After the pcall , check if there were any errors. If so, try loading the data again.

Loading

  1. set nv.Value = true right after you set all your values for the player.

Though if you are looking for a simpler way to store data I would reccommend checking out ProfileService, which makes storing player data a whole lot easier. (A lot of developers use this too!)

Edit:

I’m sorry I did not address this sooner. Apparently I had an oversight and the “Player Data Loaded” variable is actually not waiting arbitrarily for the data to load in at all.

It’s just delaying Player data loading in & the “Loaded” value getting set!
Hopefully you managed to fix it already :pray:

3 Likes

I’m sorry but I think this script has more problems than just data being lost. You’re constantly spawning a thread when a Player joins but these threads are never cleaned up? A lot of these threads also seem pointless since connections are already deferred (I don’t know if this is the correct term, but connections are not linear.)

Reading Value objects is also an interesting choice instead of having a dedicated module to get/set table data that’s cleaned up on leave.

Also, formatting is with three tildes ``` (and on most platforms, language afterwards, just not the devforum) then new line.

Thank you, il try that haven’t actually thought of this.

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