Issue With Comparing Tables

I’ll just get right to the point, so I’m firing a function to compare 2 different tables for the purpose of checking if they are the same. Although for some reason it returns “nil” instead of the correct response.

local function CheckForLatestTable(OldDataTable)
	for I, V in pairs(PlrDataTable) do 
		
		-- Checks if the category exists on the old table
		if not OldDataTable[I] then
			return false -- Plr has an old table
		else
			for i, v in pairs(V) do
				
				-- Checks if the item exists on the old table
				if not OldDataTable[i] then
					return false
				else
					return true
				end
			end
		end
	end
end

“PlrDataTable” and “OldDataTable” are similar, this function is to determine if the “OldDataTable” has gone out of date (aka new items wered added to the game) by comparing it to the new “PlrDataTable”.

The “PlrDataTable” looks something like this, it has the main table, followed by Categories, and finally the actual items. I’m trying to make sure that “OldDataTable” has ALL of these categories and items within it. If the tables are the same, it should print true, else it should be printing false but instead it prints “nil”.

local PlrDataTable = {
	
	Helmets = {
		BlueHelmet = false,
		GreenHelmet = false,
		RedHelmet = false
	},
	
	Armour = {}, -- These tables will also have items in them
	
	Clothing = {}, --
	
	Tools = {} --
	
}

Any help is greatly appreciated.

If I processed this correctly, I think you actually need to index the first key in the old data table, followed by the second key in the else if for loop. Not sure if it makes sense in words, but you could try this and see if it makes a difference:

local function CheckForLatestTable(OldDataTable)
	for I, V in pairs(PlrDataTable) do 
		
		-- Checks if the category exists on the old table
		if not OldDataTable[I] then
			return false -- Plr has an old table
		else
			for i, v in pairs(V) do
				
				-- Checks if the item exists on the old table
				if not OldDataTable[I][i] then --//Reference the first key before the second
					return false
				else
					return true
				end
			end
		end
	end
end

Hmm, for some reason it doesn’t even make it that far.

image

“hi2” never prints but “hi1” does.

If hi2 never prints I think that means the table is empty; there’s nothing to iterate over.

If that’s the case, you could return false as well, since nothing currently exists in that table.

local function getTableCount(t)
local count = 0

for _, _ in pairs(t) do
count += 1
end

return count
end

if getTableCount(V) == 0 then
    return false
end