How to compare a table's content with another table's content?

Hey, i am creating a little game, where you need to click the generated pattern, and i got it nice, but i got a problem; You can’t compare a table with other table, like:

if table == table2 then

and as there is no :GetChildren for tables, i dont know how to do that. here is my code lol:

local buttons = workspace.Colors:GetChildren()
local startButton = workspace.StartButton
local pattern = {}
local customPattern = {}
local timesClicked = 0
local red = workspace.Colors.Red.ClickDetector
local green = workspace.Colors.Green.ClickDetector
local yellow = workspace.Colors.Yellow.ClickDetector
local blue = workspace.Colors.Blue.ClickDetector
local canClick = true
local yTurn = false

red.MouseClick:Connect(function()
	if canClick then
	table.insert(customPattern, "Red")
		timesClicked += 1
		print(customPattern)
	end
end)
green.MouseClick:Connect(function()
	if canClick then
	table.insert(customPattern, "Green")
		timesClicked += 1
		print(customPattern)
	end
end)
yellow.MouseClick:Connect(function()
	if canClick then
	table.insert(customPattern, "Yellow")
		timesClicked += 1
		print(customPattern)
	end
end)
blue.MouseClick:Connect(function()
	if canClick then
	table.insert(customPattern, "Blue")
		timesClicked += 1
		print(customPattern)
	end
end)

local function checkPatterns()
	if timesClicked == #pattern then
		canClick = false
		if customPattern == pattern then
			print("Nice!")
		else
			print("Wrong!")
		end
	end
end

local function createHigh()
	local val = 1
	for i = 1, #pattern do
		local high = Instance.new("Highlight")
		high.Parent = pattern[val]
		high.Adornee = pattern[val]
		high.FillTransparency = 0.7
		val += 1
		wait(1)
		high:Destroy()
		wait(1)
	end		
end

local function createPattern()
	local randomButton = buttons[math.random(1, #buttons)]
	table.insert(pattern, randomButton)
	createHigh()
	yTurn = true
	while timesClicked < #pattern do
		if yTurn == true then
			canClick = true
		end
		wait()
		print("timesclicked "..timesClicked,"#pattern"..#pattern)
	end
end


startButton.ClickDetector.MouseClick:Connect(function()
	table.clear(customPattern)
	createPattern()
	checkPatterns()
	print(pattern)
	canClick = false
	timesClicked = 0
end)

Ty!

4 Likes

Try this:

local function tComparison(table1, table2)
   for i1, value1 in pairs(table1) do
      for i2, value2 in pairs(table2) do
          if value1 ~= value2 or i1 ~= i2 then
             return false  
         end
      end
   end
   return true
end

You’re essentially looping thru both tables and you’re comparing each one.

So it goes thru i = 1 or the first value of the first table and it compares all the values of table2 with that value and then it moves on to the next value in the first table and it repeats until it finishes comparing the first table’s last value to all the second table’s values

I know it sounds confusing but that’s the only good way to explain it

1 Like

sorry, im kinda new to scripting, so do i replace that with the checkPatterns()? And if no, where do i apply the function?

1 Like

I guess? I don’t have much context here but it only works if it’s two tables

1 Like

I mean, where do i apply that function?

2 Likes

yeah yeah I know that but I meant like I don’t know what you’re doing with the checkPatterns() function so I’m not sure if you should replace it

1 Like

Is that function what you were using to try to compare the tables?

1 Like

Wait oh I see it here now.

***no no don’t get rid of the function just add the function I provided and replace the above with this:

if tComparison(customPattern, pattern) then
1 Like

Wait oh I’m dumb :man_facepalming:

I made it so that it compares every value so it eventually will not be correct lemme fix my original function

1 Like

Okay I fixed the function now

local function tComparison(table1, table2)
   for index, value in pairs(table1) do
      if value ~= table2[index] then
			return false
		end
   end
   return true
end

This time I’m just going thru the first table and using the index, I get the value with the same position from the second table and compare them

I just go thru the first table here

This is comparing the current value from the first table “value” and I’m comparing it to the value with the same index

When you use Square Braces next to a table, you’re accessing a value from it… here’s an example:

local table = {
   -- value 1
   ["v1"] = true
   -- value 2
   ["v2"] = false
}

print(table["v1"])
-> true

print(table[1])
-> true

print(table[2])
-> false

when you just straight up use a number, you’re accessing the value that matches up with it

so imagine it like this:

local t = {
   [[ 1 ]]
   "value"
}

so when you try t[1] it will print value

Each time you add something to the list, it takes the amount of items in the table and adds a one

So if there was 4 items in the table and you added another one: it would add 1 to 4 and that new item would have an index (the number you use to reference it) of 5

sorry for all the yap… I just added an explanation just in case you wanna understand how it works (tried not to overdo it tho)

2 Likes
local function areTablesEqual(t1, t2)
    if #t1 ~= #t2 then return false end --make sure arrays are equal length
    for i, v in ipairs(t1) do --iterate over first array
        if v ~= t2[i] then return false end --compare values
    end
    return true --arrays are equal
end