I’ve searched the forums on comparing tables and I have found some methods but uses JSONEncode.
I was wondering if there is an easier way to compare tables rather then:
--> For every item in the current inventory do...
for _, item in pairs(ListInventory:GetChildren()) do
--> Now we loop through player inventory...
local exist = false
--> Grabbing those tables
for _, invItem in pairs(PlayerData.Inventory) do
--> now we start loopin through the properties...
local similar = true
for property, value in pairs(invItem) do
if typeof(value) ~= "table" then
if value ~= item[property] then
similar = false
end
else
for subproperty, subvalue in pairs(value) do
if typeof(subvalue) ~= "table" then
if subvalue ~= item[property][subproperty] then
similar = false
end
else
for subsubproperty, subsubvalue in pairs(subvalue) do
if subsubvalue ~= item[property][subproperty][subsubproperty] then
similar = false
end
end
end
end
end
end
end
end
I was thinking of creating a function but then I would still find my self nesting more and more if statements.
I’m not sure if there is a way to go through everything inside a table (including subvalues) as well.
For what you’re trying to do, I would recommend using a recursive function that loops through the table and calls itself every time it finds a value in the table that refers to another table, to loop through that one as well. I can write out the code a little later if you need it, but does that make sense?
18:14:58.292 - ServerScriptService.Script:2: stack overflow
18:14:58.518 - Stack Begin
18:15:14.243 - Script 'ServerScriptService.Script', Line 2 - global hi (x16382)
18:15:14.244 - Script 'ServerScriptService.Script', Line 7
18:15:14.244 - Stack End
I have an inventory system on the client and the client has the ability to use items, since my items have many subtables and subvalues I need to make sure the item is exact.
This checks if the item exists and if it’s the correct item before using it.
if anyone else has wants an idea of how to make this function, i made one here:
function matchTables(tbl1, tbl2)
local function scanTable(tbl1, tbl2)
for index, value in pairs(tbl1) do
if type(value) ~= "table" then
if value ~= tbl2[index] then return false end
elseif type(value) == "table" and type(tbl2[index]) == "table" then
return scanTable(value, tbl2[index])
else
return false
end
end
return true
end
return scanTable(tbl1, tbl2)
end
Another solution I thought of is to salvage each table merging each index (so this way I can compare easily) with this:
module.salvageTables = function(Table)
local salvagedContents = {}
for Index, Value in Pairs(Table) do
if TypeOf(Value) ~= "table" then
salvagedContents[Index] = Value
else
local SalvagedContents = module.salvageTables(Value)
if SalvagedContents then
for subIndex, subValue in Pairs(SalvagedContents) do
salvagedContents[Index..subIndex] = subValue
end
end
end
end
for i, v in pairs(salvagedContents) do
print(i, v)
end
return salvagedContents
end
module.salvageTables({
["Test"] = 1,
["Test2"] = {
["Test1"] = 1,
["Test3"] = {
["Test5"] = 5
}
}
})