I want to get the ‘Weapos’ arrays contents aswell as the contents of all the tables inside of the ‘Armor’ dictionary,
I tried to see is there a way to only get the same type of values as the “sword”, but it turns out I don’t know what type it is since it is just a name inserted into the table or array.
local function setupInventory(Player)
local Profile = Datastore[Player]
local Inventory = Profile.Data.Items
local PlrGui = Player:WaitForChild("PlayerGui")
local InventoryMainGui = PlrGui:WaitForChild("Inventory")
local InventoryGui = InventoryMainGui:WaitForChild("MainFrame"):WaitForChild("Inventory")
local armoritems = Profile.Data.Items.Armor
local WeaponIntems = Profile.Data.Items.Weapons
if type(Inventory) == "table" then
for _ , v in pairs(Inventory) do
if typeof(v) == "String" then
print(v)
end
end
end
end
local function setupInventory(Player)
local Profile = Datastore[Player]
local Inventory = Profile.Data.Items
local PlrGui = Player:WaitForChild("PlayerGui")
local InventoryMainGui = PlrGui:WaitForChild("Inventory")
local InventoryGui = InventoryMainGui:WaitForChild("MainFrame"):WaitForChild("Inventory")
local armoritems = Profile.Data.Items.Armor
local WeaponIntems = Profile.Data.Items.Weapons
for _ , v in pairs(Inventory) do
if type(v) == "table" then --/ Armors & Weapons
for _, v2 in pairs(v) do
if type(v2) == "string" then --/ This will print swords
print(v2)
elseif type(v2) == "table" then --/Chestplate & Helmet & Legging
for _, v3 in pairs(v2) do
--/ This will print all content in Chestplate, Helmet and Leggings tables
print(v3)
end
end
end
end
end
end
local function setupInventory(Player)
local Profile = Datastore[Player]
local Inventory = Profile.Data.Items
local PlrGui = Player:WaitForChild("PlayerGui")
local InventoryMainGui = PlrGui:WaitForChild("Inventory")
local InventoryGui = InventoryMainGui:WaitForChild("MainFrame"):WaitForChild("Inventory")
local armoritems = Profile.Data.Items.Armor
local WeaponIntems = Profile.Data.Items.Weapons
--[[
{
Armors = {
Leggings = {"string"},
Chestplate = {"string"},
Helmet = {"string"}
},
Weapons = {
"Swords"
}
}
]]
function getAllStringContentFromTable(tbl)
local stringContent = {}
for _, v in pairs(tbl) do
if type(v) == "table" then
for _, v2 in pairs(getAllStringContentFromTable(v)) do
table.insert(stringContent, v2)
end
elseif type(v) == "string" then
table.insert(stringContent, v)
end
end
return stringContent
end
for _, v in pairs(getAllStringContentFromTable(Inventory)) do
print(v)
end
end
Let’s me explain how this work. The script call “getAllStringContentFromTable(Inventory)” and the function loop the “Inventory” table. Each element defined as “v”. The “type(v) == “string”” check if the element type is a string. If it return true the function will store a string in “stringContent” table. Now let’s look at “type(v) == “table””. It check if the element is a table if it return true it call “getAllStringFromTable(v)” again. Example if “v” is a “Chestplate” or “Helmet” or “Leggings” and we call "getAllStringContentFromTable(v), the function will check each element in “Chestplate” or “Helmet” or “Leggings” is a string. It store a string and return all the strings. Then the
for _, v2 in pairs(getAllStringContentFromTable (v)) do
table.insert(stringContent, v2)
end
Will loop and insert the return value to “stringContent” table. Finally the function return all the stored string and
for _, v in pairs(getAllStringContentFromTable(Inventory)) do
print(v)
end
Print all the string content. This is just like a loop explaination but I hope you understaand
This is simple yet nice, but here’s an even nicer way to do it, which adds in data types for him.
This isn’t shorter, but it’s more effective if you want to do something with the data afterwards, like setting up the players character/giving items, etc.
local function setupInventory(Player)
local Profile = Datastore[Player]
local Inventory = Profile.Data.Items
local PlrGui = Player:WaitForChild("PlayerGui")
local InventoryMainGui = PlrGui:WaitForChild("Inventory")
local InventoryGui = InventoryMainGui:WaitForChild("MainFrame"):WaitForChild("Inventory")
local Items = {}
--[[
Items Format:
Items = {
Entry format {
Type = "Armor" or "Weapons",
SubType = "Chestplate" or "Helmet" or "Leggings" or "None", (None is for Weapons, which have no SubType)
Index = any,
Value = any
},
...
}
]]
for Type, Data in pairs(Inventory) do
if Type == "Armor" then
for SubType, SubData in pairs(Data) do
for Index, Value in pairs(SubData) do
local Entry = {
Type = Type,
SubType = SubType,
Index = Index,
Value = Value
}
table.insert(Items, Entry)
end
end
elseif Type == "Weapons" then
for Index, Value in pairs(Data) do
local Entry = {
Type = Type,
SubType = "None",
Index = Index,
Value = Value
}
table.insert(Items, Entry)
end
end
end
print(Items)
return Items
end
You shouldn’t define getAllStringContentFromTable in setupInventory…
Just define it once outside of it so that it can be used later and doesn’t need to be recreated each time.