Saving [Value] Data Inside myTable:GetChildren()

local RS = game:GetService(“ReplicatedStorage”):FindFirstChild(“SubmitCode”)

local DSS = game:GetService(“DataStoreService”)
local DataStore = DSS:GetDataStore(“DataDATADATA”)

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

local stats = player:WaitForChild("ChatColor")
local playerKey = "ChatColor_"..player.UserId

local itemTable = {}

for index, item in pairs(stats:GetChildren())do
	itemTable[item.Name] = item.Name     -- saving the item name to item name to be able to load again!
	
	
end

local success, errormessage = pcall(function()
	DataStore:SetAsync(playerKey, itemTable)
end)

if errormessage then
	print("Fail To Save!")
else
	
	if success then
		print("saving successfully!")
		
	end
	
end

end)

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

local folder = Instance.new("Folder", player)
folder.Name = "ChatColor"

local playerKey = "ChatColor_"..player.UserId
local codesData
local success, errormessage = pcall(function()
	codesData = DataStore:GetAsync(playerKey)
end)

if success then
else
	print("Avatar failed to load!")
	warn(errormessage)

end

if type(codesData) == "table" then
	for index, value in pairs(codesData) do
		
	
		
		
		local b = Instance.new("BoolValue")   -- what you create, string, int or folder needs the same, 
		
		b.Name = value 					-- using value or any variables codesData is the value, you can either use v or value, 
		b.Parent = player.ChatColor			-- parenting it to the folder you wish to save!
		
		print(b.Name.."Saved successfully!")
		
		
	end
end	

end)

Provide an overview of:

  • What does the code do and what are you not satisfied with?
    I already know how to save both Name’s and Value’s, but I don’t know how to save the VALUE data in a table…

  • What potential improvements have you considered?
    The code I have above can only save the names, I tried other methods, but I always got an error, I search up online, but none of them can save the data, I know there is, but it can only save the value if they already have parented the folder and the children to a folder, example a leaderstats, the stage, coins have already parented so its easily to save them using Coins.Value = Data.Coins, but it doesnt work on table, I know I am asking an obvious question but I am beginner just started last march 2 and this is the first time I have trouble understanding this, sorry for my English

What you need to do is, instead of using a array as a table, you should use a dictionary.
When you use the GetAsync function, this is what will be returned:

local data = nil
local success, errr = pcall(function()
     data = DataStore:GetAsync(playerKey)
    
    


end)

if data then
    print(data)
end


What will be printed is this:

["itemName1"] = "Cheese";
["itemName2"] = "sausage"

Maybe this could help you out!

Thank you for your answer, but I already know this, this only saved the name not the value, I am trying to save a Value from an instance,

Oh, I guess that is not possible, you can not save entire instances. I guess you have to save all of the neceassary things from the Instances you have made, so that you can GetAsync when you need it again, do you understand?

yep, I understand, but to tell you honestly, I already found my solution, I just want to find another opinion or solution, this is the solution that I’ve found, it can store the Name and also the Value, Source, thank you for the info too!

1 Like