Make sure data does not reset

Hey Dev Forum

For a while now I have noticed that whenever a player’s data is unable to save it usually just resets. Is their a way I can make sure it doesn’t reset?

Function to Save Data

local function Save(Plr)
    print("Attempted to saved player please wait for data response")
    
    local PlrStats = CreateTable(Plr)
    local Sucess, Error = pcall(function()
        local PlrUserID = "Player_"..Plr.UserId
        PlayerData:SetAsync(PlrUserID, PlrStats)
    end)
    if not Sucess then 
        warn("Not Able To Save")
        
    else 
        print("Sucessfully Saved "..Plr.Name)
    end
end

Entire Data Script

local DSS = game:GetService("DataStoreService")
local PlayerData = DSS:GetDataStore("RaceData001")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local function CreateTable(Plr)
	local PlrStats = {}
	for _,V in ipairs(Plr.RData:GetChildren()) do 
		PlrStats[V.Name] = V.Value
	end
	return PlrStats
end

local function Save(Plr)
	print("Attempted to saved player please wait for data response")
	
	local PlrStats = CreateTable(Plr)
	local Sucess, Error = pcall(function()
		local PlrUserID = "Player_"..Plr.UserId
		PlayerData:SetAsync(PlrUserID, PlrStats)
	end)
	if not Sucess then 
		warn("Not Able To Save")
		
	else 
		print("Sucessfully Saved "..Plr.Name)
	end
end

Players.PlayerAdded:Connect(function(Plr)
	local RData = Instance.new("Folder", Plr)
	RData.Name = "RData"

	local RaceRolls = Instance.new("IntValue")
	RaceRolls.Parent = RData
	RaceRolls.Name = "RaceRolls"
	RaceRolls.Value = 5
	
	local Race = Instance.new("StringValue")
	Race.Parent = RData
	Race.Name = "RaceData"
	Race.Value = "Human"

	

	local PlrUserId = "Player_"..Plr.UserId
	local StoreData 
	local Sucess, Error = pcall(function()
		StoreData = PlayerData:GetAsync(PlrUserId)
	end)
	if not Sucess then 
		warn("Not Able To Save")
	end

	if type(StoreData) == "table" then
		for I,V in pairs(StoreData) do
			if RData:FindFirstChild(I) then
				RData:FindFirstChild(I).Value = V
			end
		end
	end
end)

Players.PlayerRemoving:Connect(function(Plr)
	Save(Plr)
end)

game:BindToClose(function()
	for _, Plr in ipairs(Players:GetPlayers()) do
		Save(Plr)
	end
end)

GlobalDataStore GetAsync docs state that it returns a Tuple (multiple arguments) - "This function returns the latest value of the provided key and a DataStoreKeyInfo"
Have you validated the type of value you’re getting back to if it is indeed of type table?

Print out the typeof(StoredData) as well as the data itself to validate what you’re getting back.

I would imagine if this fails, the user would end up with default data on load.
GlobalDataStore:GetAsync (roblox.com)

Yes thats the problem it fails and it loads the default values. it usually happens when the player leaves or just entered the game.

As for if I have validated the type of value I’m getting back is a table I printed store data and it did come back as a table.

I have no clue exactly tho how I would go about making sure this doesn’t happen

I see nothing wrong with the code itself.

Again I think the first place you need to look is the data you’re getting back.

Literally do print(StoreData) and check it out in studio.
Also do the same when you’re saving the data print(PlrStats)