How to script a dictionary

I have multiple folders in server storage which represent a soldiers information, I’m trying to pack it all up and send it to the client. All of their information are number values and things like that. So how can i put that all into a table? The end result should look somewhat like this

Table{
     )-Soldier1
          )-Strength
          )-Health
     )-Soldier2
          )-Strength
          )-Health
     )-Soldier3
          )-Strength
          )-Health
}

print(Table.Soldier3.Health)
local exampleDictionary = {
  ["animals"] = {
        ["example1"] = "dog",
        ["example2"] = "cat"
    }
}

print(exampleDictionary["animals"]) -- This would print the value associated with the key called "animals", which is the table that has "example1" = "dog" and "example2" = "cat"
1 Like

how would I insert the information from the soldiers info folder into that table? They have a number value representing their health, strength, etc. How can that all put scripted into a dictionary

By using a special function: table.insert:

You could just do something like…

local t = path:GetDescendants()

This would return everything under “path”.

1 Like

I know but how do I insert all of their information into the dictionary, you would have to loop through each soldier and insert the soldier into the table, then insert all that soldiers information into the soldiers own table. I dont get how i could do that.

1 Like

I can only give this example as I don’t know how you have setup your game and how it functions but it would be something similar to this:

for _, v in pairs(table:GetChildren()) do
    table.insert(table, soldierinfo)
end
1 Like
local Table = {}
for i,v in pairs(All_The_Soldiers:GetChildren()) do
    table.insert(Table, v)
    for i,Info in pairs(v.Information:GetChildren()) do
        table.insert(Table.v, Info.Value)
    end
end

this is what is going through my head, like obviously this script dosnt work, but you have to get all the soldiers to have their own table inside “Table” so that they can put their information inside themselves to achieve the end result i posted all the way above.

1 Like

Can you send a picture of the game’s hierarchy where the soldiers, the needed information and the script are located?

1 Like

Their information:
image
Theres multiple of them:
image
all these guys gotta have their information sent in one nice dictionary to the client, because the client cant see all this stuff since its in server storage

1 Like

The script won’t work because of this line:

You are trying to insert some information inside a Solider instance. You should create individual tables for each soldiers like so:

local soldiersTable = {}

for _, soldier in ipairs(ServerStorage.Soldiers:GetChildren()) do
    local soldierTable = {}
    table.insert(soldiersTable, soldierTable)
    for property, value in pairs(soldier.Information) do
        soldierTable.Name = solder.name
        -- insert all the properties and their respective values here
    end
end

Since your soldier’s information are in the form of value instances, you have to rescript the script to suit your preferences.

2 Likes

This is still quite weird, I don’t get which one is “Soldier1” and I can’t see “Health” nor “Strength”, I would guess that each “ArmyInfo” is one soldier and “Health” and “Strength” are two IntValues/NumberValues, I’ll build up on that and write some code real quick, correct my information if anything was wrong.

1 Like

I believe the OP should take our ideas and concepts and then apply those to its own script based on how their hierachy works since they are the only one who knows best of it.

1 Like

He did try and couldn’t so better off writing simple code and he can tweek it here and there to fit his needs, a base if that makes more sense.

1 Like

Not sounding too conceited but I’ve already provided a base for the OP to make reference on, but sure you can share us your two cents.

1 Like

Here you go:

local armyStuff -- folder containing your `ArmyInfo`

local function getDictionary()
    local t = {}

    for i, v in pairs(armyStuff:GetChildren()) do -- looping through all the `ArmyInfo`s
        t[`Solder{i}`] = {
            Strength = v.Strength.Value,
            Health = v.Health.Value
        }

        -- `Solder{i}` names it as Solder and adds the number `i` at the end,
        -- where i is a number, if your `ArmyInfo` has the name, change it.

        -- The values are self explanatory, ask if your confused tho.
    end

    return t
end
3 Likes

Would just like to point out something, here, at the top, your inserting an empty table then adding the “properties” to it, meaning the tables will always be empty, I would guess that this surely wasn’t intentional.

1 Like

No? Adding an empty table inside another table doesn’t mean the content inside it will always be empty, that table will still be existed in the main table and I can just insert values into it.

1 Like

True but you would need to index the last item in the main table which in most cases isn’t what happens, you did create a variable so it’s either you created a useless variable or you placed the insert in a wrong position.

1 Like

Thank you both for the help, this is exactly what I was looking for. I really appreciate it.

1 Like