Saving folder contents in Data Store

I’m trying to save all the children of a folder using data store. basically, this folder will be in the player. In it will be all boats they have unlocked. How would I save all children of this folder to data store

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"
	
	local Boats = Instance.new("Folder")
	v.Name = "Boats"
	v.Parent = player
	
	local Equipped = Instance.new("StringValue")
	a.Name = "Equipped"
	a.Parent = player
	
	local Loot = Instance.new("Folder")
	b.Name = "Loot"
	b.Parent = player
	
	local data1
	local data2
	local data3
	local success, errormessage = pcall(function()
		data1 = myDataStore:GetAsync(player.UserId.."-boats")
		data2 = myDataStore:GetAsync(player.UserId.."-equipped")
		data3 = myDataStore:GetAsync(player.UserId.."-loot")
	end)
	
	if success then
		v.Value	= data1
		a.Value = data2
		b.Value = data3
		print("Successfuly saved data")
	else
		print("There was an error whilst getting your data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
	myDataStore:SetAsync(player.UserId.."-boats",player.leaderstats.Boats.Value)
	myDataStore:SetAsync(player.UserId.."-equipped",player.leaderstats.Equipped.Value)
	myDataStore:SetAsync(player.UserId.."-loot",player.leaderstats.Loot.Value)
	end)
	
	if success then
		print("Player data successfully saved!")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end
end)

end)

The only way you could do it is by saving the names because datastores can only store integers and strings. The way you could do that is just

local data = {}

local datastore = gane:GetService("DataStoreService"):GetDataStore("Data")

for i,v in pairs(folder:GetChildren()) do
  data[#data+1] = v.Name
end

datastore:SetAsync(key,data)

1 Like

Thanks, I will try this! (30 characters), could you show me how i would insert this into the script i pasted above?

There can be improvements made into your data saving system. U should put all the data into one table and store it in one Key.

You should also keep the server open when saving data

1 Like

I only know how to do it this way, as I am not an advanced scripter

Tell me why your name sounds familiar

Borealis Studios, Co owner, Main builder