How would i save children of value

How would I save this:

Screenshot_17

I want to save both the parent and the children of the parent. The values aren’t added on player added like leaderstats because you get them with in the progress of the game. I already have a parent saving script but i’m not sure about the children. Here’s the script right now

local Players = game:GetService("Players")

local dataStoreService = game:GetService("DataStoreService")
local dataKey1 = dataStoreService:GetDataStore("songstore")

Players.PlayerAdded:Connect(function(player)
	
	local boolsFolder = Instance.new("Folder", player)
	boolsFolder.Name = "Bools"
	
	local songsFolder = Instance.new("Folder", boolsFolder)
	songsFolder.Name = "Songs"
	
	local playerkey = "bools_"..player.UserId.."_store"
	dataKey1:GetAsync(playerkey)
	
	local data = dataKey1:GetAsync(playerkey)
	--local data2 = dataKey:GetAsync(equippedPlayerKey)

	print(data)
	
	if data ~= nil then
		for i, v in pairs(data) do
			local val = Instance.new("BoolValue")
			val.Name = v
			val.Parent = songsFolder
		end
	end
end)

Players.PlayerRemoving:Connect(function(player)
	
	local songsTable = {}
	
	for i, v in pairs(player.Bools.Songs:GetChildren()) do
		table.insert(songsTable, v.Name)
			print(songsTable)
			end
	
	local playerkey = "bools_"..player.UserId.."_store"
	 dataKey1:SetAsync(playerkey, songsTable)
end)
1 Like

To save a group of values, you can use tables.
So, you have a table in a table to save the values.

local DataToSave = {

Song1Name = {
Song1Value1Name = Value1.Value
Song1Value2Name = Value1.Value2.Value
Song1Value3Name = Value1.Value3.Value
};

Song2Name = {
Song2Value1Name = Value1.Value
Song2Value2Name = Value1.Value2.Value
Song2Value3Name = Value1.Value3.Value
};

}
1 Like

Yes, that could work, but the values are added proggresively in the game so it would need for both the name of the value to be saved to make a new instance and also save the .Value

1 Like

For this, you could loop through the directory where all the values are saved, say, theyre all in a folder called “songs”
You would create a new table, loop through the folder and then add the values.

local DataToSave = {}
for i, v in pairs(player.songs:GetChildren()) do
-- v.Name is the songs name
-- so if the value is called "Camellia - Exit This Earths Atomosphere" the table would be called the same
DataToSave[v.Name] = {
Value1 = v.Value;
Value2 = v.Value2.Value;
Value3 = v.Value2.Value;
}
end

You could also just save a table, and not name it.

for i, v in pairs(player.songs:GetChildren()) do
table.insert(DataToSave, {Value1 = v.Value; Value2 = v.Value2.Value; Value3 = v.Value2.Value;})
end
1 Like

Yes that works but, how would i do instance.New here so i could also recreate the children and parent them to the value here:

	if data ~= nil then
		for i, v in pairs(data) do
			local val = Instance.new("BoolValue")
			val.Name = v
			val.Parent = songsFolder
		end
	end
end)

(data is the datastore:GetAsync)

I think i might need another key to the same if data doesnt = nil here

Considering the way ive saved the data in my code samples, the values are labelled in the table.
So, if the saved table is
{SongName = "TQBF - The Big Black"; SongData = "0, 1, 0, 0, 1"}
then loading it would look like:

-- for i, v bla bla bla do
local NewSong = Instance.new("BoolValue", songsFolder)
NewSong.Name = v.SongName -- This would set the Values Name to "TQBF - The Big Black"
local SongData = Instance.new("StringValue", NewSong)
SongData.Name = "SongData"
SongData.Value = v.SongData
1 Like

Maybe instead of putting all the data in the stringvalue something like this would work:

	if data ~= nil then
		for i, v in pairs(data) do
			local val = Instance.new("BoolValue")
			val.Name = v
			val.Parent = songsFolder
			for i, c in pairs(v:GetChildren()) do
				local val1 = Instance.new("BoolValue")
				val1.Name = c
				val1.Parent = val
			end
		end
	end
end)

Players.PlayerRemoving:Connect(function(player)
	
	local songsTable = {}
	
	for i, v in pairs(player.Bools.Songs:GetChildren()) do
		table.insert(songsTable, {Value1 = v.Value; v.InAlbum.Value; v.placeholder.Value})
			print(songsTable)
			end
	
	local playerkey = "bools_"..player.UserId.."_store"
	 dataKey1:SetAsync(playerkey, songsTable)
end)

In this case, v is already a table, so :GetChildren() isnt needed there, as :GetChildren() returns a table from an instance, and you would most likely get an error if you try to get a table from a table.

Anyway, its 12am for me, so im going to get off now, and i hope what i provided does help you make your game :slight_smile:

Ok, thanks a lot for the help :+1:

After messing around, i finally got a script to work.

With instances too. :

local Players = game:GetService("Players")

local dataStoreService = game:GetService("DataStoreService")
local dataKey1 = dataStoreService:GetDataStore("PlaceholderSongs10") -- used: 1, 2, 3, 4, 5, 6, 7 , 8, 9

Players.PlayerAdded:Connect(function(player)
	
	local boolsFolder = Instance.new("Folder", player)
	boolsFolder.Name = "Bools"
	
	local songsFolder = Instance.new("Folder", boolsFolder)
	songsFolder.Name = "Songs"
	
	local playerkey = "bools_"..player.UserId.."_store"
	dataKey1:GetAsync(playerkey)
	
	local data = dataKey1:GetAsync(playerkey)
	--local data2 = dataKey:GetAsync(equippedPlayerKey)

	print(data)
	
	if data ~= nil then
		for i, v in pairs(data) do
			print(v[1], v[2], v[3])
			--for i, c in pairs(v) do
				--print(c[1], c[2], c[3])
			local songData1 = Instance.new("StringValue", songsFolder)
			songData1.Name = v[1]
			local imgData = Instance.new("StringValue", songData1)
			imgData.Name = v[2]
			local inAlbumData = Instance.new("StringValue", songData1)
				inAlbumData.Name = v[3]
				--end
		end
end
end)

Players.PlayerRemoving:Connect(function(player)
	
	local songsTable = {}
	
	for i, v in pairs(player.Bools.Songs:GetChildren()) do
		table.insert(songsTable, {v.Name,v.ImgID.Name,v.InAlbum.Name})
			print(songsTable)
			end
	
	local playerkey = "bools_"..player.UserId.."_store"
	 dataKey1:SetAsync(playerkey, songsTable)
end)

(the lines which are cleared were me messing around)