Datastore randomly resets

My datastore keeps randomly resetting, idk if its a loading issue or what but any help?

game.Players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("Folder", plr)
	stats.Name = "stats"

	local Data = nil

	pcall(function()
		Data = ds:GetAsync(Prefix)
	end)
	if Data ~= nil then
		for i,v in pairs(stats:GetChildren()) do
			v.Value = Data[v.Name]
		end
	else
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local Prefix = plr.UserId

	local leaderstats = plr.stats

	pcall(function()
		if leaderstats then
			local SaveData = {}
			
			for i,v in pairs(leaderstats:GetChildren()) do
				SaveData[v.Name] = v.Value
			end

			ds:SetAsync(Prefix, SaveData)
		end
	end)
end)

Are your api services turned on?

yeah, it happens randomly, 0 clue why

Did you forget this?

local DSS = game:GetService("DataStoreService")
local ds = DataStoreService:GetDataStore("PlayerData")
1 Like

I forgot it in the post, but i have it in my script

`

local ds = game:GetService(“DataStoreService”):GetDataStore(“newdatastore”)

`

Do you have any form of auto save / manual save? If this is all you have for data saving it may reset just because of lack of saving and or some sort of data loss.

game.Players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("Folder", plr)
	stats.Name = "stats"

	local Data = nil

	pcall(function()
		Data = ds:GetAsync(Prefix)
	end)
	if Data ~= nil then
		for i,v in pairs(stats:GetChildren()) do
			v.Value = Data[v.Name]
		end
	else
	end
end)

Never defined Prefix.

Sorry again, in the original code, i forgot to put this

local Prefix = plr.UserId

That’s inside game.Players.PlayerAdded:Connect(function(plr), right?

Correct its right under it in the script

Try printing the data once you load it to find out if it’s loading.
This script was working just fine for me.

local ds = game:GetService("DataStoreService"):GetDataStore("newdatastore1")

game.Players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("Folder", plr)
	stats.Name = "stats"
	local Prefix = plr.UserId

	local Data = nil

	pcall(function()
		Data = ds:GetAsync(Prefix)
	end)
	
	if Data ~= nil then
		for i,v in pairs(stats:GetChildren()) do
			v.Value = Data[v.Name]
		end
	else
		print("Is nil")
	end
	print(Data)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local Prefix = plr.UserId

	local leaderstats = plr.stats

	pcall(function()
		if leaderstats then
			local SaveData = {}

			for i,v in pairs(leaderstats:GetChildren()) do
				SaveData[v.Name] = v.Value
			end
			print(SaveData)
			ds:SetAsync(Prefix, SaveData)
		end
	end)
end)

your pcall currently doesn’t do much except, basically ignoring errors, and if it errors, the data won’t get saved.

try letting the pcall return if the save was successful or not, example:

game.Players.PlayerRemoving:Connect(function(plr)
	local Prefix = plr.UserId

	local leaderstats = plr:FindFirstChild("stats")
	if leaderstats then

		local function saveData()
			local success, fail = pcall(function()
				local SaveData = {}
				for i,v in pairs(leaderstats:GetChildren()) do
					SaveData[v.Name] = v.Value
				end

				ds:SetAsync(Prefix, SaveData)
			end)

			if not success then -- retry if failed
				wait()
				saveData()
			end
		end
		
		saveData()
	end
end)

I’d also suggest having the pcall return success or fail when you are loading data

Have you tried using a pcall(function() this would narrow down where and what is wrong with the code, to be making it reset on it’s own.

How would I do this when I loaded in data?

Your problem is you don’t have a game:BindToClose function, that fires when the server shuts down. This results in your data not saving so it gets reset. Just loop through all the players in the bindToClose function, and save their data.

Another issue with your code is, that datastores are web requests, meaning they can randomly fail from time to time. If the pcall does not return success, keep trying until it does, alternatively you could have a maxAttempts.

2 Likes