How can I check table to see if they are the same?

So I have a big table and I copy it to make modifications to it. I want to print all things that are not the same as the other values

e.g

local Table = {
	V1 = Vector3.new(1, 1, 1),
	V2 = Vector3.new(10, 10, 10),
	V3 = Vector3.new(100, 100, 100)
}

local Table2 = {
	V1 = Vector3.new(1, 1, 1),
	V2 = Vector3.new(20, 20, 20),
	V3 = Vector3.new(100, 100, 100)
}

--Coding here to find the differce in values. Output:

--[[
	V2 = Vector3.new(10, 10, 10) ~= V2 = Vector3.new(20, 20, 20)
--]]

If anyone help I would be grateful

PLEASE HELP ME IT WOULD BE GOOD FOR ME AND YU

BY THE WAY I DONT WANT THE OUTPUT

  V1 = 1, 1, 1 ~= V2 = 20, 20, 20
  V1 = 1, 1, 1 ~= V3 = 100, 100, 100
  V2 = 10, 10, 10 ~= V1 = 1, 1, 1
  V2 = 10, 10, 10 ~= V2 = 20, 20, 20
  V2 = 10, 10, 10 ~= V3 = 100, 100, 100
  V3 = 100, 100, 100 ~= V1 = 1, 1, 1
  V3 = 100, 100, 100 ~= V2 = 20, 20, 20

Here’s a quickly thrown-together function to compare tables:

local function equals(t1, t2)
	for key, value in pairs(t1) do
		if (t2[key] ~= value) then
			return false
		end
	end
	-- check for values in t2 that aren't in t1 as well
	for key, value in pairs(t2) do
		if (t1[key] ~= value) then
			return false
		end
	end
	return true
end

And here is a recursive version for nested tables:

local function equals(t1, t2)
	local function subset(a, b)
		for key, value in pairs(a) do
			if (typeof(value) == "table") then
				if (not equals(b[key], value)) then
					return false
				end
			else
				if (b[key] ~= value) then
					return false
				end
			end
		end
		return true
	end
	return subset(t1, t2) and subset(t2, t1)
end
2 Likes

Don’t worry I think I done it myself have I?

local Table = {
	V1 = Vector3.new(1, 1, 1),
	V2 = Vector3.new(10, 10, 10),
	V3 = Vector3.new(100, 100, 100)
}

local Table2 = {
	V1 = Vector3.new(1, 1, 1),
	V2 = Vector3.new(20, 20, 20),
	V3 = Vector3.new(100, 100, 100)
}

for I, V in pairs(Table) do
	for I2, V2 in pairs(Table2) do
		if V ~= V2 and I == I2 then
			print(I, "=", V, "~=", I2, "=", V2)
		end
	end
end

Your code is using nested iteraations, so for every value in Table, it’s iterating through all of Table2 which is probably not what you want. What my code does is iterate through just one of the tables, and checks if the other table has a matching key-value pair. If they all match then we know that Table is a subset of Table2. Then we do the same thing with Table2. If Table and Table2 are subsets of each other we can conclude that they are identical.

This is what Imtrying to do but its not working

local ReplicatedStorage= game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local AK12 = require(ReplicatedStorage.GunModules.AK12)

function LoopTable(Table)
	local String = ""
	for Key, Value in pairs(Table) do
		String = String .. Key .. " = " .. Value .. "\n"
	end
	return String
end

function TableEquality(Table, Table2)
	for I, V in pairs(Table) do
		for I2, V2 in pairs(Table2) do
			if V ~= V2 and I == I2 then
				print(I, "=", V, "~=", I2, "=", V2)
			end
		end
	end
end

local function ShallowCopy(Table)
	local Table2 = {}
	for Key, Value in pairs(Table) do
		Table2[Key] = Value
	end
	return Table2
end

local Original = ShallowCopy(AK12)

warn(LoopTable(Original))

local UnOriginal = AK12

UserInputService.InputBegan:Connect(function(InputBegan)
	if InputBegan.KeyCode == Enum.KeyCode.Insert then
        warn(LoopTable(UnOriginal))
		print("Insert Pressed. File Saved!")
	end
end)
print("Script Ran!")

Please help me triumph through this problem