Need help with loading old data from old datastores

Problem

  • The problem is I had old datastore, but it was really bad, cause was using 4 datastores to save all the data.
  • I tried to load old data, to new datastore, but the problem is it detects when there’s old data for player, but isn’t loading the data for player (I tested it)

Script

Stats Script
local http = game:GetService("HttpService")
local datastore = game:GetService("DataStoreService")
local database = datastore:GetDataStore("Stats_Database_1")
local oldCrateDatabase = datastore:GetDataStore("Crate_DataStore_1")
local oldCoinsDatabase = datastore:GetDataStore("CoinsDataStoreSaveHmmCuzWhyNot")
local oldRobuxDatabase = datastore:GetDataStore("RobuxDataStore_3")
local oldStageDatabase = datastore:GetDataStore("StageDataLOL")
local plrs = game:GetService("Players")

local function saveData(plr)		
	local success, errormessage = pcall(function()
		database:SetAsync(plr.UserId, {coins = plr.Coins.Value; robux = plr.RobuxDonated.Value; crates = plr.CratesOpened.Value; stage = plr.leaderstats.Stage.Value})
	end)
	
	if success then
		print("Saved all stats for "..plr.Name.."!")
	else
		print("Failed to save all stats for "..plr.Name.."!")
	end
end

local function getData(plr)	
	local playerStats
	local oldStats = {coins = nil; crates = nil; robux = nil; stage = nil;}
	
	local success, errormessage = pcall(function()
		playerStats = database:GetAsync(plr.UserId)
	end)
	
	if success and playerStats then
		print("Loaded all stats for "..plr.Name.."!")
		
		plr.Coins.Value = playerStats.coins
		plr.RobuxDonated.Value = playerStats.robux
		plr.CratesOpened.Value = playerStats.crates
		plr:WaitForChild("leaderstats").Stage.Value = playerStats.stage
		
	elseif not errormessage then
		oldStats.coins = oldCoinsDatabase:GetAsync(plr.UserId, "-coins")
		oldStats.crates = oldCrateDatabase:GetAsync(plr.UserId, "-crates")
		oldStats.robux = oldRobuxDatabase:GetAsync(plr.UserId, "-robux")
		oldStats.stage = oldCoinsDatabase:GetAsync(plr.UserId)
		
		wait()
		
		plr.Coins.Value = oldStats.coins
		plr.RobuxDonated.Value = oldStats.robux
		plr.CratesOpened.Value = oldStats.crates
		plr.leaderstats.Stage.Value = oldStats.stage
		
		wait(10)
		
		oldCoinsDatabase:RemoveAsync(plr.UserId, "-coins")
		oldCrateDatabase:RemoveAsync(plr.UserId, "-crates")
		oldRobuxDatabase:RemoveAsync(plr.UserId, "-robux")
		oldCoinsDatabase:RemoveAsync(plr.UserId)
		
		print("Loading old data for "..plr.Name.."!")
	else
		print("Failed to load all stats for "..plr.Name.."!")
	end
end

plrs.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local crates = Instance.new("NumberValue")
	crates.Name = "CratesOpened"
	crates.Parent = plr
	
	local rs = Instance.new("NumberValue")
	rs.Name = "RobuxDonated"
	rs.Parent = plr
	
	local coins = Instance.new("NumberValue")
	coins.Name = "Coins"
	coins.Parent = plr
	
	local stage = Instance.new("NumberValue")
	stage.Name = "Stage"
	stage.Parent = leaderstats
		
	getData(plr)
end)

plrs.PlayerRemoving:Connect(function(plr)
	saveData(plr)
end)

game:BindToClose(function()
	for _, v in pairs(plrs:GetChildren()) do
		saveData(v)
	end
end)

Thanks for your help! :slightly_smiling_face:

There’s a few things I noticed.

if the data fails to load, there is nothing there to allow it to retry which means that even if a player has data the data might end up not loading and then being erased due to your script (Big issue, data loss can be caused).

– Something I use when loading data

for i = 1,10 do -- Max number of attempts is 10 here
     local Success, Error = pcall(function()
         -- Attempts to load data
     end)
     wait(3) -- Allows time to load and helps reduce ques
     if Success then
         break -- Stops the loop as it has loaded
     end
     if Error then
         print("DataStore Error, Attempt #".. i .. ": \n" .. Error)
     end
end

-- If there is no data try for old data using the same method
-- If still no data they are a new player
-- DO NOT ERASE OLD DATA IF IT IS NOT FOUND

Ok I’ll try to edit my script.

So it would look like this?

local function getData(plr)	
	local playerStats
	local oldStats = {coins = nil; crates = nil; robux = nil; stage = nil;}
	
	for i = 1,10 do
		local Success, Error = pcall(function()
			playerStats = database:GetAsync(plr.UserId)
			
			oldStats.coins = oldCoinsDatabase:GetAsync(plr.UserId, "-coins")
			oldStats.crates = oldCrateDatabase:GetAsync(plr.UserId, "-crates")
			oldStats.robux = oldRobuxDatabase:GetAsync(plr.UserId, "-robux")
			oldStats.stage = oldCoinsDatabase:GetAsync(plr.UserId)
		end)
		wait(3)
		if Success then
			print("Loaded all stats for "..plr.Name.."!")
			plr.Coins.Value = playerStats.coins
			plr.RobuxDonated.Value = playerStats.robux
			plr.CratesOpened.Value = playerStats.crates
			plr:WaitForChild("leaderstats").Stage.Value = playerStats.stage
			
			break			
		elseif not playerStats or oldStats.coins ~= nil or oldStats.crates ~= nil or oldStats.robux ~= nil or oldStats.stage ~= nil then
			plr.Coins.Value = oldStats.coins
			plr.RobuxDonated.Value = oldStats.robux
			plr.CratesOpened.Value = oldStats.crates
			plr.leaderstats.Stage.Value = oldStats.stage			
			
			wait(60)
			
			oldCoinsDatabase:RemoveAsync(plr.UserId, "-coins")
			oldCrateDatabase:RemoveAsync(plr.UserId, "-crates")
			oldRobuxDatabase:RemoveAsync(plr.UserId, "-robux")
			oldCoinsDatabase:RemoveAsync(plr.UserId)
						
			print("Loading old data for "..plr.Name.."!")
			break
		elseif not playerStats then
			print("New player has joined the game!")
		end			
		if Error then
			print("DataStore Error, Attempt #".. i .. ": \n" .. Error)
		end
	end
end

Edit: I found bug, ill fix it (fixed)

@minimic2002 ok ill go test the script, if it will work ill give you solution.