DataStore2 Help

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I followed a DataStore2 tutorial on youtube, link provided below, however it saves newly made instances in the folder, I want it to save already existing values that I create manually myself instead.

For example, have a “copy folder” of all the possible instances I want to exist under the “Folder” that’s being saved. (I’m trying to make it so I can save multiple twitter codes and etc.)

  1. What is the issue? Include screenshots / videos if possible!
    Tutorial followed: How To Save Values And Tables To DataStore2 - YouTube

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried making a Folder in ReplicatedStorage, and in the DataStore2 code, I try to copy all of the children in that folder into the “actual folder” that would be saved and exists in the player.

local DataStore2 = require(1936396537)

local defaultCashValue = 0 -- bobux
local CodesTable = {}

game.Players.PlayerAdded:Connect(function(player)
	print("Data Loaded")
	
	local CashDataStore = DataStore2("CashDataStore", player)
	---
	local CodeDataStore = DataStore2("CodesDataStore", player)
	
	
	local Cash = Instance.new("IntValue")
	Cash.Parent = player
	Cash.Name = 'Bobux'
	
	local Folder = Instance.new("Folder")
	Folder.Parent = player
	Folder.Name = 'Codes'
	
	---
	for i, x in pairs(game.ReplicatedStorage.InfoData:GetChildren()) do
		if x then
			local newrep = Instance.new("NumberValue")
			newrep.Parent = Folder
			newrep.Name = x.Name
		end
	end
	---
	
	local function cashUpdate(value)
		Cash.Value = CashDataStore:Get(value)
	end
	----
	local function codeUpdate(value)
		CodesTable = CodeDataStore:Get(value)
		Folder:ClearAllChildren()
		
		for _, code in pairs(CodesTable) do
			local InfoValue = Instance.new("BoolValue")
			InfoValue.Parent = Folder
			InfoValue.Name = code
		end
	end
	
	
	
	cashUpdate(defaultCashValue)
	codeUpdate(CodesTable)
	CashDataStore:OnUpdate(cashUpdate)	
	CodeDataStore:OnUpdate(codeUpdate)
	
	
end)




game.ReplicatedStorage.DataUpdate.OnServerEvent:Connect(function(player, info, rewardvalue, datatype)
	
	local DataStore = DataStore2(datatype, player)
	
	

	if datatype == nil then -- check if its table saving or not// nil = not
		if info == 'Code1' then
			DataStore:Increment(rewardvalue, defaultCashValue)
		end
		
	else -- saving to a table/accessing table
		local Codes = player.Codes
	
		
		local codeTable = {}
		for _, v in ipairs(Codes:GetChildren()) do
			table.insert(codeTable, v.Value)
		end
		
		DataStore:Set()
	end
	
end)  

image

This was my method of doing it, not sure if its proper, but cloning everything under the InfoData folder into the actual folder in the player then saving any data that would exist inside that folder

Hello,

You are able to save and get a data table by saving and getting the data table itself.
I recommend using table.insert() and table.remove() for editing your data table.

When you want to cache the data, or input the data into a folder like you did, you can do this after your script gets the data.

If you want to change your data, call the data table, unwrap the content, and edit that individual data. Wrap it back into a table and save.

You can find more detail about saving dictionaries or tables here:

Hello! I looked into the article you sent and not sure if my code is properly done, I tried to implement the method used.

local DataStore2 = require(1936396537)

local defaultCashValue = 0 -- bobux
local CodesTable = {}

game.Players.PlayerAdded:Connect(function(player)
	print("Data Loaded")
	
	local CashDataStore = DataStore2("CashDataStore", player)
	---
	local CodeDataStore = DataStore2("CodesDataStore", player)
	
	
	local Cash = Instance.new("IntValue")
	Cash.Parent = player
	Cash.Name = 'Bobux'
	
	local Folder = Instance.new("Folder")
	Folder.Parent = player
	Folder.Name = 'Codes'
	
	---
	local function setupUserData()
		local  userData = {

			Codes = {	
				["Code1"] = 1;
				["Code2"] = 1;
			};


			Items = {

				["Pets"] = {
					["Dog"] = {nil};
					["Cat"] = {nil};
					["Wolf"] = {nil};
				};

			};


		}
		return userData
	end
	
	local userData = DataStore2(CodeDataStore, player):Get(setupUserData())
	---
	
	local function cashUpdate(value)
		Cash.Value = CashDataStore:Get(value)
	end
	----
	local function codeUpdate(value)
		CodesTable = CodeDataStore:Get(value)
		Folder:ClearAllChildren()
		
		for _, code in pairs(CodesTable) do
			local InfoValue = Instance.new("BoolValue")
			InfoValue.Parent = Folder
			InfoValue.Name = code
		end
	end
	
	
	
	cashUpdate(defaultCashValue)
	codeUpdate(CodesTable)
	CashDataStore:OnUpdate(cashUpdate)	
	CodeDataStore:OnUpdate(codeUpdate)
	
	
end)




game.ReplicatedStorage.DataUpdate.OnServerEvent:Connect(function(player, info, rewardvalue, datatype)
	
	local userData = DataStore2(datatype, player)
	
	

	if datatype == 'CodesDataStore' then -- check if its table saving or not// nil = not
		if info == 'Code1' then
			userData["Codes"]["Code1"] = userData["Codes"]["Code1"] + 2
			DataStore2(datatype, player):Set(userData)
		end
		
	else -- saving to a table/accessing table
		local Codes = player.Codes
	
		
		local codeTable = {}
		for _, v in ipairs(Codes:GetChildren()) do
			table.insert(codeTable, v.Value)
		end
		
		userData:Set()
	end
	
end)  

(Apologies, DataStore was never my strong suit)

I’d say this would work but I can’t test the code myself.
If it’s saving your data and loading your data properly, then you’ve successfully made DataStore2 work.

The only thing I recommend is caching your data and only updating that cache, not the DataStore itself, since calling Get() would cause server network issues, and force DataStore2 to bottleneck events.
When the player leaves, you can make it save. You can also make it save automatically every 5 minutes or so.

Hi, I am also looking for tutorial online about datastore2 tables but did you make it work? I tried this one but it got an error.

I actually switched to use ProfileService instead, its easily configurable for me and haven’t had any issues

1 Like