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)
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
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
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
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
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)