Saving values to Datastore from a folder

  1. What do you want to achieve?
    I want to make a Datastore that takes values in a folder and saves all the values.

  2. What is the issue?
    It doesn’t save the values that are in the folder.

  3. What solutions have you tried so far?
    I saw the forum post similar to this one but it didn’t help me fix my problem.

local Datastore = game:GetService("DataStoreService")
local MyDataStore = Datastore:GetDataStore("MyDataStore")
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

Players.PlayerAdded:Connect(function(Player)
	
	local PlayerData = ServerStorage:WaitForChild("PlayerData"):Clone()
	PlayerData.Parent = Player
	
	local SavedData = {}
	
	for i, v in pairs(PlayerData:GetChildren()) do 
		table.insert(SavedData, v)	
	end
	
	for i, v in pairs(PlayerData:GetChildren()) do 
		local Data = MyDataStore:GetAsync(Player.UserId..SavedData[i].Name)
		v.Value = Data	
	end
	
end)

Players.PlayerRemoving:Connect(function(Player)
	
	local PlayerData = Player:WaitForChild("PlayerData")
	local SavedData = {}

	for i, v in pairs(PlayerData:GetChildren()) do 
		table.insert(SavedData, v)	
	end
	
	for i, v in pairs(SavedData) do
		MyDataStore:SetAsync(Player.UserId..SavedData[i].Name, SavedData[i].Value)
	end
	
end)
for i, v in pairs(SavedData) do
	MyDataStore:SetAsync(Player.UserId..SavedData[i].Name, SavedData[i].Value)
end

You are trying to save an instance to a datastore, which isn’t possible. You could, however, save the name of the instance. Just do: MyDataStore:SetAsync(user, v.Name)

Also, note that in a for i, v in pairs(Table) loop, you don’t need to do “Table[i],” you simply write “v.”