How do I convert a folder containing values into a table?

So im trying to be more efficient with my work, and so instead of having a lot of data stores, i can just use one. The loading in seems to work fine, but i don’t really know how i would convert a folder with values in it into a table. Could someone help me?

local https = game:GetService("HttpService")
local ds = game:GetService("DataStoreService")
local pds = ds:GetDataStore("PlayerData")
local module = {}

module.NewPlayerData = {
	Currency = {
		Coins = 0;
		Gems = 0;
	};
	AllTime = {
		TotalCoins = 0;
		TotalGems = 0;
		TotalCoinsCollected = 0;
		TotalGemsCollected = 0;
		TotalDamage = 0;
		TotalKills = 0;
		TotalBossKills = 0;
		TotalDistanceTraveled = 0;
		TotalTimePlayed = 0;
	};
	Stats = {
		CoinsMultiplier = 0;
		GemsMultiplier = 0;
		DamageMultiplier = 0;
	};
	CharacterStats = {
		Walkspeed = 16;
		Jumppower = 50;
	};
	About = {
		Banned = nil;
		Bans = 0;
		Warnings = 0;
	};
};

function module.SetUp(Player)
	local data
	if pds:GetAsync(Player.UserId) ~= nil and pds:GetAsync(Player.UserId)~="" then
		data = https:JSONDecode(pds:GetAsync(Player.UserId))
	else
		data = module.NewPlayerData
	end
	for _, v in pairs(data) do
		if typeof(v)=="table" then
			local folder = Instance.new("Folder",Player)
			folder.Name = _
			for a, x in pairs(v) do
				if tonumber(x)~=nil then
					local val = Instance.new("IntValue",folder)
					val.Name = tostring(a)
					val.Value = x
				end
			end
		else
			print("Non table")
			print(v)
		end
	end
end

function module.Pack(Player)
	local data = {}
	local sessiondata = {}
	for _, v in pairs(Player:GetChildren()) do
		if v:IsA("Folder") then
			for a, x in pairs(v:GetChildren()) do
				
			end
		end
	end
	for _, v in pairs(module.NewPlayerData) do
		if typeof(v)=="table" then
			
		end
	end
	print(https:JSONEncode(Player))
end

return module

Thanks

Unless you’ve already gotten down how to make it into a table and are looking for better ways to do it, the Scripting Support section might be better suited for your needs.

local table = {}
for i, v in ipairs(folder:GetChildren) do
if v:IsA(“NumberValue”) then
table.insert(table, v.Value)
end
end

2 Likes

I would have done it as a dictionary with table[v.Name] = v.Value;

Just so you have the value stored and know for sure which object it is, in case you add or remove objects in game for any reason and don’t want it to overwrite them, or use the wrong data.

4 Likes

There are responses around the DevForum for doing things like this. See the following:

The above post supplies a function that converts a folder to a dictionary and returns it so you can use it for saving or whatever purposes you have. Just be wary of the value types you have. For example, if you have an ObjectValue, the reference to the model will still be added to the returned dictionary but it cannot be saved to a DataStore.

1 Like