Need help moving from datastore to datastore2

So I’m trying to move my game to Datastore2 as a I experienced some data loss with regular datastores.
I know how to save numbers for ingame currency and etc. but have no clue how to save stringvalues for ingame items.
For now I’ve got this code for regular datastore:

game.Players.PlayerRemoving:Connect(function(player) 
	
	local data = {} 
	
	data.Coins = player.Coins.Value
	
	data.EquippedKnife = player.EquippedKnife.Value
	data.EquippedSkin = player.EquippedSkin.Value
	
	data.Skins = {}
	data.Knives = {}
	
	for i, v in pairs(player.SkinInventory:GetChildren()) do
		table.insert(data.Skins,v.Name)
		print(v.Name)
	end
	
	for i, v in pairs(player.KnivesInventory:GetChildren()) do
		table.insert(data.Knives,v.Name)
		print(v.Name)
	end
	
	local success, errorMsg = pcall(function()
	DataStore:SetAsync(player.UserId,data)
end)
	
	if success then
		print("Successfully Saved!")
	end

	
	
end)

game:BindToClose(function()
	
	for i, player in pairs(game.Players:GetPlayers()) do
		
					local data = {} -- Coins, Equipped Items, Skins, Knives
				
				data.Coins = player.Coins.Value
				
				data.EquippedKnife = player.EquippedKnife.Value
				data.EquippedSkin = player.EquippedSkin.Value
				
				data.Skins = {}
				data.Knives = {}
				
				for i, v in pairs(player.SkinInventory:GetChildren()) do
					table.insert(data.Skins,v.Name)
					print(v.Name)
				end
				
				for i, v in pairs(player.KnivesInventory:GetChildren()) do
					table.insert(data.Knives,v.Name)
					print(v.Name)
				end
				
				local success, errorMsg = pcall(function()
				DataStore:SetAsync(player.UserId,data)
			end)
				
				if success then
					print("Successfully Saved!")
		end	
		wait(2)
			
	end
end)

I would really appreciate your help!

1 Like

Firstly I am assuming you already have downloaded the datastore2 api.
This is how I would set it up.
https://gyazo.com/0cf12926065f87ca14307c00e59fbb9e
[You only need the Core_Server and Datastore2 module in server script service]

--Require the datastore2 module
local Data = require(script.data_Module2)

--Data.Combine IS IMPORTANT
--The first value "TestingData" is the name of datastore you are using
--Meaning switching "TestingData" to "TestingData1" would use a different datastore (startover)

--Any other value you add needs to be added similar to "Wins"
Data.Combine("TestingData", "Wins")

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		--Events you want to run whenever a players character joins/loads
		--Character.Parent = workspace.Players <= Example putting player in a folder
	end)
	
    --Making a new datastore value to save
	-----Wins-------------------------------
--First Attach a Number Value to the player titled "Wins"
--This is used mainly by the client to easily check their current value
	local Wins = Instance.new("NumberValue", Player)
	Wins.Name = "Wins"
	
--Setting up the actual datastore
	local WINStore = Data("Wins", Player)
--:Get(0) here means if they have no "Wins" data it will be set to 0
	local WINAmnt = WINStore:Get(0)
	
--Setting the player attached value
	Wins.Value = WINAmnt

--This updates the player value whenever the datastore changes
	WINStore:OnUpdate(function(newValue)
		Wins.Value = newValue
	end)
	----------------------------------------
	
	--I usually grant awards here as this runs on joining
	--awardBadge(Player, 2124607448)
end)
--Example for changing data or getting data
--ASSUMING you already found and required the Datastore2 api
--local Data = require(game:GetService("ServerScriptService)".Core_Server.data_Module2)
--First get the datastore
local Store = Data("Wins", Player)

--TO SET
--Or in this case set the players wins to 10
Store:Set(10)

--TO GET
local WinCount = Store:Get(0)
--This works for boolean and other values as well (Store:Get(true))

HOPE THIS HELPS
Best Regards

  • Mark

As I understand, here you saving numbers. But what I need is, to save StringValues. This is what I’ve got when player joins the game:

game.Players.PlayerAdded:Connect(function(player)
	


local gameCoins = Instance.new("IntValue")
gameCoins.Name = "Coins"
gameCoins.Value = 0 
gameCoins.Parent = player

local knivesInventory = Instance.new("Folder")
knivesInventory.Name = "KnivesInventory"
knivesInventory.Parent = player

local skinInventory = Instance.new("Folder")
skinInventory.Name = "SkinInventory"
skinInventory.Parent = player

local equippedKnife = Instance.new("StringValue")
equippedKnife.Name = "EquippedKnife"
equippedKnife.Parent = player

local equippedSkin = Instance.new("StringValue")
equippedSkin.Name = "EquippedSkin"
equippedSkin.Parent = player
	
	


	
	

	local data
	
	local success, errorMsg = pcall(function()
		data = DataStore:GetAsync(player.UserId)
	end)
	
	if data ~= nil then
		
		if data.EquippedKnife then
			equippedKnife.Value = data.EquippedKnife
		end
		
		if data.EquippedSkin then
			equippedSkin.Value = data.EquippedSkin
		end
		
		if data.Coins then
			gameCoins.Value = data.Coins
		end
		
		if data.Skins then
			for i, v in pairs(data.Skins) do
				local val = Instance.new("StringValue")
				val.Name = v
				val.Parent = skinInventory
			end	
		end
		
		if data.Knives then
				for i, v in pairs(data.Knives) do
				local val = Instance.new("StringValue")
				val.Name = v
				val.Parent = knivesInventory
			end
		end
		
	end
	
	game.ReplicatedStorage.SendData:FireClient(player,data)
end)

Anyone has an idea how I could save Strings?

I’ve solved it. No more help needed!