(SOLVED) How to send a folder full of information to the server?

It only gets the direct children of the Demands Folder :confused:

local function folderToDictionary(folder)
    local dictionary = {}

    for _, object in ipairs(folder:GetChildren()) do
        if object:IsA("Folder") then
            dictionary[object.Name] = folderToDictionary(object)
        elseif object:IsA("ValueBase") or object.ClassName:match("Value") then
            dictionary[object.Name] = object.Value
        end
    end

    return dictionary
end

1 Like

would changing folder:GetChildren() to folder:GetDescendants() fix it?

2 Likes

Defiantly not, that would turn it into one big clump of information, it wouldn’t keep the structure.

1 Like

Perhaps what you’re really looking for is a way to convert the values to a table? Sending entire instances over isn’t helpful because since they aren’t replicated to the server, you will only have copies(if you decide to serialize for example), and changing the values there won’t also change them on the client(unless you use remotes and change the client-sided values).

Here’s a function that can convert the format of instances you provided to a table:

type valTypes = (boolean | BrickColor | CFrame | Color3 | number 
	| Instance | Ray | string | Vector3)

local function folderToTable(folder: Instance): {[string]: valTypes}
	local t = {}
	for i, v in pairs(folder:GetChildren()) do
		if v:IsA("Folder") then
			t[v.Name] = folderToTable(v)
		elseif v:IsA("ValueBase") then
			t[v.Name] = v.Value
		end
	end
	return t 
end

You can run it on the client and send the table to the server instead.

Keep in mind:

  1. The function won’t function properly if 2 instances have the same name under the same folder.
  2. If one of the values is nil, the key associated with it won’t exist within the table.
  3. For this to work you will have to convert Break Alliance, GoldDemand and War Reperations to folders.

Here’s an example of accessing the data of the table:

--For instances that don't have spaces:
local goldSummaryDirectory = data.GoldDemand.SummaryDirectory
--for instances with spaces:
local country = data["Break Alliance"].Country
1 Like

Thanks for the response but theres gonna be a lot of instances with the same name. Also im so close to actually making this work, im just confused on one question

local function folderToDictionary(folder)
    local dictionary = {}

    for _, object in ipairs(folder:GetChildren()) do
        if object:IsA("Folder") then
            dictionary[object.Name] = folderToDictionary(object)
        elseif object:IsA("ValueBase") or object.ClassName:match("Value") then
            dictionary[object.Name] = folderToDictionary(object) ----- QUESTION HERE 
         --this recursive adds objects' children but how do i add object.Value along with it???
        end
    end

    return dictionary
end

image

as you can see they all have their children, but are missing their values

is there no way to give “1” a name? going Something.NumberValue.1 will error out

For the elseif do v.Value instead, and as I mentioned change all values that act as folders to folders. If you store any data in them(for example if GoldDemand except of children also has a value), make it a new value under said folder.

For example GoldDemand becomes a folder but now has 2 values, SummaryDirectory and Gold which was the original GoldDemand value.

Is there also a way to make it so it dosnt say the objects name twice. As you can see it says “War reoperations” twice, how could i make it so the children of the 2nd war reparations are just placed in the first one?

Solved it, i just turned the Value into an actual instance and used the regular code :+1: