Saving descendants of a folder

Hi, this is a continuation from: How can I save all descendants of a folder? - Help and Feedback / Scripting Support - DevForum | Roblox

I never actually got it to work. I know where the error is, please can someone tell me how to fix it?

This script is in ServerScriptService, @loyanafirst helped me create this:

local datastoreService = game:GetService("DataStoreService")
local datastore = datastoreService:GetDataStore("Eggz101")

local players = game:GetService("Players")
local sStorage = game:GetService("ServerStorage")

players.PlayerAdded:Connect(function(player)

	local plrFolder = Instance.new("Folder")
	plrFolder.Name = "DiscoveredEggs"
	plrFolder.Parent = player

	local success, errorMessage = pcall(function()
		datastore:GetAsync(tostring(player.UserId))
	end)
	
	if success then
		if datastore:GetAsync(tostring(player.UserId)) then
			print("Data exists")
			for _, data in pairs(datastore:GetAsync(tostring(player.UserId))) do
				print(data[1])
				local instance = Instance.new("BoolValue")
				instance.Name = data[1]
				instance.Value = data[2]
				instance.Parent = player.DiscoveredEggs
				for _, v in pairs(data.children) do----------------ERROR IS HERE (I'm pretty sure)
					local child = Instance.new("BoolValue")
					child.Name = v[1]
					child.Value = v[2]
					child.Parent = player.DiscoveredEggs:FindFirstChild(instance.Name)
				end
			end
		else
			print("No data exists")
			for _, egg in pairs(sStorage.DiscoveredEggs:GetChildren()) do
				print(egg)
				local instance = Instance.new("BoolValue")
				instance.Value = false
				instance.Name = egg.Name
				instance.Parent = player.DiscoveredEggs
				for _, v in pairs(egg:GetChildren()) do
					print(v)
					local child = Instance.new("BoolValue")
					child.Value = false
					child.Name = v.Name
					child.Parent = player.DiscoveredEggs:FindFirstChild(instance.Name)
				end
			end
		end
	end
end)

players.PlayerRemoving:Connect(function(player)
	print("Player removing")
	local tab = {}
	for _, egg in pairs(player.DiscoveredEggs:GetChildren()) do
		local childrenTab = {}
		for _, child in pairs(egg:GetChildren()) do
			table.insert(childrenTab, {child.Name, child.Value})
		end
			table.insert(tab,{egg.Name, egg.Value, childrenTab})
	end

	local suc, err = pcall(function()
		datastore:SetAsync(tostring(player.UserId), tab)
	end)
	if not suc then
		print(err)
	end
	print(datastore)
end)

Feel free to ask as many questions as you need. All help is appreciated!

Did you try resetting your data? It might be because of invalid data that is already in the datastore.

1 Like

Yeah, I reset it. Thanks for replying, it errors: ‘invalid argument #1 to ‘pairs’ (table expected, got nil)’

with the modifications you made it should be:

for _, v in pairs(data[3])
1 Like

THANK YOU, THANK YOU SO MUCH!!! You don’t understand how much you have helped me!!

you’re welcome. this way i also learn to be a better scripter. good luck.

1 Like