How can I switch from DataStore to DataStore2 without losing data?

(hey this question is off topic & I apologize if it sounds stupid)
but wouldn’t

datastore:SetAsync(arg1, arg2);
--arg1 = key 
--arg2 = data

be more efficient if just arg1 = plr.UserId;
instead of parsing an arbitrary string?
arg1 = “player_” … plr.UserId; ← used currently

It doesn’t matter, you can use both.

1 Like

Bro, I’m sorry, but the topic was created to help and not to write scripts instead of someone

2 Likes

I understand, I will try to do it anyway, thank you very much.

Hello alex i tried your script it does save but it does not load my old data , any reason?
old script as indicator

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataSaver001")
local Players = game:GetService("Players")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Money = Instance.new("IntValue")
	Money.Name = "Money"
	Money.Parent = leaderstats

	local success, data = pcall(function()
		return DataStore:GetAsync(player.UserId.."-Money")
	end)

	if success then
		Money.Value = data or 3000
		print(player.Name .. " has " .. Money.Value .. " money.")
	else
		warn("Error while loading Money data: " .. data)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, error = pcall(function()
		DataStore:SetAsync(player.UserId.."-Money", player.leaderstats.Money.Value)
		print(player.Name .. "'s money data saved. Current money: " .. player.leaderstats.Money.Value)
	end)

	if success then
		print("Player data saved successfully!")
	else
		warn("Error while saving Money data: " .. error)
	end
end)

current script ( problem )

local DataStoreService = game:GetService("DataStoreService")
local oldDataStore = DataStoreService:GetDataStore("DataSaver001")
local newDataStore = require(script.DataStore2)
newDataStore.Combine("DATA2", "player") -- better do not change DATA2 for testing only

local function create_table(player)
	local player_stats = {}
	for _, stat in pairs(player.leaderstats:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end
	return player_stats
end

local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = leaderstats

	local MoneyStats = newDataStore("DATA2", player)
	local data = MoneyStats:Get(player.UserId.."-Money")
	if not data then 
		local success, oldData = pcall(function()
			return oldDataStore:GetAsync(player.UserId.."-Money")
		end)
		if success then
			data = oldData or {Money=3000} 
			MoneyStats:Set(player.UserId.."-Money", data)
		else
			warn("Error stupewddd " .. oldData)
			data = {Money=3000} 
		end
	end

	money.Value = data['Money']

	money.Changed:Connect(function(value)
		MoneyStats:Set(player.UserId.."-Money", create_table(player))
	end)
end

local function onPlayerExit(player)
	local player_stats = create_table(player)
	local MoneyStats = newDataStore("DATA2", player)
	local success, err = pcall(function()
		MoneyStats:Set(player_stats)
		MoneyStats:Save()
	end)

	if not success then
		warn('Game problem lol')
	end
end

let me know

local data = MoneyStats:Get()

You don’t need to use the key as this is already the player’s base (local MoneyStats = newDataStore("DATA2", player))

And also here, do not use the key, only the values

MoneyStats:Set(data)

ouh i see my bad , lack of knowledge … i will try that

Try to read about DataStore2 here

https://kampfkarren.github.io/Roblox/api/

Hey alex thank you for the info
My database finally done and it works well , but im not 100% sure it will save since im still new with datastore2 , what can i add or change to make it better ?

local DataStoreService = game:GetService("DataStoreService")
local oldDataStore = DataStoreService:GetDataStore("DataSaver001") -- money DATA
local playerData = DataStoreService:GetDataStore("PlayerData") -- Vehicle DATA
local newDataStore = require(script.DataStore2)
newDataStore.Combine("DataSaver001", "PlayerData") -- combining it


--- MONEY FUNCTION OK 
local function create_table(player)
	local player_stats = {}
	for _, stat in pairs(player.leaderstats:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end
	return player_stats
end


-- MOTOR FUNCTION #1
local function CreateCarValue(player, name, value)
	local carValue = player.OwnedCars:FindFirstChild(name)
	if not carValue then
		carValue = Instance.new("BoolValue")
		carValue.Name = name
		carValue.Parent = player.OwnedCars
	end
	carValue.Value = value
	return carValue
end

--MOTOR FUNCTION #2 -- MOTOR TABLE
local function CreateTable(player)
	local playerStats = {}
	for _, stat in pairs(player.OwnedCars:GetChildren()) do
		if stat.Value == true then
			playerStats[stat.Name] = true
		end
	end
	return playerStats
end


-- MOTOR FUNCTION #3
local cars = {
	-- list cars put here ok
	"HondeEX5",
	"HondeWave100",
	"Kasaki 150sp",
	"Lagenda115zr",
	"Lajak",
	"Rzx Milinium",
	"Wave125i Drag",
	"Light Lz",
	"Yamha Soleriz",
	"Yamha 125rz",
	"Sniper150Drag",
	"Yamha Sniper135 v1",
	"Honde Deo",
	"Honde Nsr500",
	"Icikiwir",
	"Yamha Yzr500",
	"Honde Wave 125r",
	"Kasaki Pdk R1",
	"Dukatti SuperSport S",
	"Kasaki Serpico",
	"Yamha X1r",
	"Vesp 150ss",
	"Dukatti 1299",
	"Yamha Sniper v2",
	"Suzuka Raider 150",
	"Kasaki Ninja H2",
	"Yamha R6 v2",
	"Suzuka Hayabusa Drag",
	"Yamha X1r White"
}




local function onPlayerJoin(player)
	-- Money Section
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = leaderstats
	
	--- Motor Section
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "OwnedCars"
	leaderstats.Parent = player
	
	--- Money Section
	local MoneyStats = newDataStore("MoneyData1", player)
	local data = MoneyStats:Get()
	if not data then 

		local oldData = oldDataStore:GetAsync(player.UserId.."-Money")
		print("oldData for player " .. player.Name .. ": " .. tostring(oldData))
		if oldData then
			data = oldData
			MoneyStats:Set(data)
		else
			warn("Error: " .. oldData)
			data = {Money=3000} 
		end

	end

	if type(data) == "number" then
		money.Value = data
	else
		money.Value = data['Money']
	end
	money.Changed:Connect(function(value)
		MoneyStats:Set(create_table(player))
	end)
	
	
	-- Vehicle section ( not done yet )
	
	
	local MotorStats = newDataStore("VehicleData123", player)
	local data1 = MotorStats:Get()

	if data1 then
		for car, value in pairs(data1) do
			if value == true and table.find(cars, car) then
				CreateCarValue(player, car, true)
			end
		end
	else
		-- load old data
		local oldmotor = playerData:GetAsync("Player_" .. player.UserId)
		if oldmotor then
			print("Load for player " .. player.Name)

			for car, value in pairs(oldmotor) do
				if value == true and table.find(cars, car) then
					CreateCarValue(player, car, true)
				end
			end

			print("Saving for player " .. player.Name)
			MotorStats:Set(oldmotor)
		else
			print("ZZZZZ for " .. player.Name)
		end
	end
end




local function onPlayerExit(player)
	local player_stats = create_table(player)
	local motor_stats = CreateTable(player)
	local MotorStats = newDataStore("VehicleData123", player)
	local MoneyStats = newDataStore("MoneyData1", player)
	MotorStats:Set(motor_stats)
	MoneyStats:Set(player_stats)
	player.leaderstats.Money.Value = player_stats.Money
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

If my script good already then tell me !

1 Like

Hey alex! thanks a lot, I was able to do the cars one but I have a little problem with the money and Delivers one because it works but when the player saves something, it doesn’t really save anything and it only keeps the DS1 information.

How exactly does it not save? Everything worked before…

Hey dude , script runs well but whenever alot of player join at the same time , warning added to queue or data sent too frequently occur . How do i counter this?

My idea to counter = add gap between each player to let them load data and save data ( save data needed since if data not loaded yet and saved occur , need to wait data loaded first before data save ) .

Well this shouldn’t be a problem since the limit is 60+10*n and there are 10 extra requests per player, maybe you’re doing too fast or too much saving or getting data somewhere

local DataStoreService = game:GetService("DataStoreService")
local oldDataStore = DataStoreService:GetDataStore("DataSaver001") -- money DATA
local playerData = DataStoreService:GetDataStore("PlayerData") -- Vehicle DATA
local helmetData = DataStoreService:GetDataStore("Helmet") -- Helmet DATA
local newDataStore = require(script.DataStore2)
newDataStore.Combine("DataSaver001", "PlayerData") -- combining it


--- MONEY FUNCTION OK 
local function create_table(player)
	local player_stats = {}
	for _, stat in pairs(player.leaderstats:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end
	return player_stats
end


-- MOTOR FUNCTION #1
local function CreateCarValue(player, name, value)
	local carValue = player.OwnedCars:FindFirstChild(name)
	if not carValue then
		carValue = Instance.new("BoolValue")
		carValue.Name = name
		carValue.Parent = player.OwnedCars
	end
	carValue.Value = value
	return carValue
end

--MOTOR FUNCTION #2 -- MOTOR TABLE
local function CreateTable(player)
	local playerStats = {}
	for _, stat in pairs(player.OwnedCars:GetChildren()) do
		if stat.Value == true then
			playerStats[stat.Name] = true
		end
	end
	return playerStats
end


-- MOTOR FUNCTION #3
local cars = {
	-- list cars put here ok
	"HondeEX5",
	"HondeWave100",
	"Kasaki 150sp",
	"Lagenda115zr",
	"Lajak",
	"Rzx Milinium",
	"Wave125i Drag",
	"Light Lz",
	"Yamha Soleriz",
	"Yamha 125rz",
	"Sniper150Drag",
	"Yamha Sniper135 v1",
	"Honde Deo",
	"Honde Nsr500",
	"Icikiwir",
	"Yamha Yzr500",
	"Honde Wave 125r",
	"Kasaki Pdk R1",
	"Dukatti SuperSport S",
	"Kasaki Serpico",
	"Yamha X1r",
	"Vesp 150ss",
	"Dukatti 1299",
	"Yamha Sniper v2",
	"Suzuka Raider 150",
	"Kasaki Ninja H2",
	"Yamha R6 v2",
	"Suzuka Hayabusa Drag",
	"Yamha X1r White",
	"Mz Agusza F4",
	"Honde NR750",
	"Dukatti 1199 Martini"
}




local function onPlayerJoin(player)
	-- Money Section
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = leaderstats
	
	--- Motor Section
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "OwnedCars"
	leaderstats.Parent = player
	
	--- Money Section
	local MoneyStats = newDataStore("MoneyData1", player)
	local data = MoneyStats:Get()
	
	
	if not data then 

		local oldData = oldDataStore:GetAsync(player.UserId.."-Money")
		print("oldData for player " .. player.Name .. ": " .. tostring(oldData))
		if oldData then
			data = oldData
			MoneyStats:Set(data)
		else
			data = {Money=3000} 
			print(player.Name .." MONEY'S DATA not found from both data , creating a new data")
		end
	end
	
	if data then
		print("money loaded for " .. player.Name)
	end

	if type(data) == "number" then
		money.Value = data
	else
		money.Value = data['Money']
	end
	money.Changed:Connect(function(value)
		MoneyStats:Set(create_table(player))
	end)
	
	
	-- Vehicle section ( not done yet )
	
	
	local MotorStats = newDataStore("VehicleData", player)
	local data1 = MotorStats:Get()

	if data1 then
		print("Motorsikal loaded forc" .. player.Name)
		for car, value in pairs(data1) do
			if value == true and table.find(cars, car) then
				CreateCarValue(player, car, true)
			end
		end
	else
		-- load old data
		local oldmotor = playerData:GetAsync("Player_" .. player.UserId)
		if oldmotor then
			print("Load for player " .. player.Name)

			for car, value in pairs(oldmotor) do
				if value == true and table.find(cars, car) then
					CreateCarValue(player, car, true)
				end
			end

			print("Saving for player " .. player.Name)
			MotorStats:Set(oldmotor)
		else
			print("NO DATA MOTORCYCLE FOUND FOR " .. player.Name)
		end
	end
end




local function onPlayerExit(player)
	local player_stats = create_table(player)
	local motor_stats = CreateTable(player)
	local MotorStats = newDataStore("VehicleData", player)
	local MoneyStats = newDataStore("MoneyData1", player)
	MotorStats:Set(motor_stats)
	MoneyStats:Set(player_stats)
	player.leaderstats.Money.Value = player_stats.Money
	print("money and motor saved for " .. player.Name)
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

its runs fine , but when alot of player join at the same time warning occur , like its really weird . if normal datastore script it doesnt give me kind of warning

there is a plugin named datastore editor

How many can join the server as much as possible?

20 player max but old one 17 player max , should i lower to 12 or 15 ? AND btw sometime data not load perfectly sometime it print nil instead of their money

I think main problem is i call async money and cars at the same time , i would like to add delay but im scared if game updated and player got no enough time load their second data which ended up dataloss due failed to load and save second data

It happens that yes, it shows the information stored in the DB1 but I think that as the info loaded in the DS2 is directly from the DS1 it is not updated but remains static.

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