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
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"
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
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.
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.
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
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.
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.
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.
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
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.
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.
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.