Table comparison help

Hello forumers! I was coding a quick puzzle system, and when I went to compare two tables, despite the indices checked being (I think) equivalent, the code believes they are not. If I am understanding the jargon, I am comparing two nodes, which have the same integers, but are being passed as inequivalent. I’ve been stuck on this for a while and thought seeking professional input would be the best action. The code is pasted below, and please bear with the inefficiency of it, it’s a miracle it works.

--Text here isn't relevant, just for continuity
local tweenservice = game:GetService("TweenService")
local soundservice = game:GetService("SoundService")

local braam = soundservice["DARK BRAAM 01"]:Clone()
braam.Parent = script.Parent.PrimaryPart
braam.PlaybackSpeed = 0.8
braam.Volume = 1.5

local puzzle = script.Parent
local numbers = puzzle.Numbers

local randomnumbers = puzzle.RandomNumbers

local mouseclick1 = puzzle.Joystick1:WaitForChild("ClickDetector")
local mouseclick2 = puzzle.Joystick2:WaitForChild("ClickDetector")
local mouseclick3 = puzzle.Joystick3:WaitForChild("ClickDetector")
local mouseclick4 = puzzle.Joystick4:WaitForChild("ClickDetector")

local mouseclickbutton = puzzle.Button.ClickDetector

local tablecolors = {
	Color3.new(1, 0.666667, 0),
	Color3.new(0.666667, 0, 1),
	Color3.new(0.666667, 1, 1)
}

local tableoperations = {
	Color3.new(0, 1, 0), 
	Color3.new(1, 1, 0)
}

local codetable = {}
local attempttable = {}

local sumtotal = 5

--Creating randomized color sequences, and finding a result 
local function gencode(numberpair)
	if sumtotal ~= 5 then
		sumtotal = 5
	end
	local operation
	if numberpair ~= nil then
		for _, v in ipairs (numberpair:GetChildren()) do
			local colorrand = tablecolors[math.random(1, #tablecolors)]
			local coloropp = tableoperations[math.random(1, #tableoperations)]
			if v.Name == "Part" then
				local number = v:FindFirstChild("NumberValue")
				v.Color = colorrand
				if v.Color == Color3.fromRGB(255, 170, 0) then
					number.Value = 1
				end
				if v.Color == Color3.fromRGB(170, 0, 255) then
					number.Value = 2
				end
				if v.Color == Color3.fromRGB(170, 255, 255) then
					number.Value = 0
				end
			else
				local bool = v:FindFirstChild("BoolValue")
				operation = v
				v.Color = coloropp
				if v.Color == Color3.new(0, 1, 0) then				
					bool.Value = true
				end
				if v.Color == Color3.new(1, 1, 0) then
					bool.Value = false
				end
			end
		end
		for _, v in ipairs(numberpair:GetDescendants()) do
			if v.Name == "NumberValue" then
				local op = operation.BoolValue
				if op.Value == true then
					sumtotal -= v.Value
				else
					sumtotal += v.Value
				end
			end
		end
		return sumtotal
	end
end

--Creates the newly randomized combination from random colors
for _, v in ipairs(randomnumbers:GetChildren()) do
	local attribute = v:GetAttribute("Order")
	local value = gencode(v)
	table.insert(codetable, {attribute, value})
end

for _, v in ipairs(codetable) do
	print(_, v)
end

local tablenumbers = {}

--Puzzle-solving mechanism
local function createnumber(nixietube, joystick)
	local tubeselect = nixietube
	if tubeselect ~= nil then
		local axis = joystick.PrimaryPart
		local tweeninfo = TweenInfo.new(0.25, Enum.EasingStyle.Quart, Enum.EasingDirection.Out, 0, true, 0.1)
		local tweentable = {CFrame = axis.CFrame * CFrame.Angles(0, 0, math.rad(-30))}
		local tween = tweenservice:Create(axis, tweeninfo, tweentable)
		tween:Play()
		tween.Completed:Connect(function()
			local value = tubeselect:FindFirstChild("Value")
			value.Value += 1
			if value.Value > 9 then
				value.Value = 0
			end
			for _, v in ipairs (tubeselect:GetChildren()) do
				if tonumber(v.Name) == value.Value then
					local meshnumber = v:FindFirstChildWhichIsA("MeshPart")
					meshnumber.Color = Color3.fromRGB(188, 110, 61)
					meshnumber.Transparency = 0
				end
				if tonumber(v.Name) == value.Value - 1 or tonumber(v.Name) == value.Value + 9 then
					local meshnumber2 = v:FindFirstChildWhichIsA("MeshPart")
					meshnumber2.Color = Color3.fromRGB(43, 25, 14)
					meshnumber2.Transparency = 0.8
				end
			end
		end)
	end
end

--Resets puzzle 
for _, v in ipairs(puzzle.Numbers:GetDescendants()) do
	if v:IsA("MeshPart") and v.Name ~= "zero" then
		v.Color = Color3.fromRGB(43, 25, 14)
		v.Transparency = 0.8
	end
end

--Checks if the puzzle solution inputted matches the code
local function solvepuzzle()
	local tblcode = {}
	local tblattempt = {}
	for _, v in ipairs(numbers:GetDescendants()) do
		if v:IsA("NumberValue") then
			local order = v.Parent:GetAttribute("Order")
			local valueattempt = v.Value
			table.insert(attempttable, {order, valueattempt})
		end
	end
	table.sort(codetable, function(a, b)
		return a[1] < b[1]
	end)
	table.sort(attempttable, function(a, b)
		return a[1] < b[1]
	end)

--[[Problems arise here, the print says that both tables give the same node (when solved correctly), yet the code treats them as not being equal. For example, if the code yields:
               ▼  {
                    [1] = 1,
                    [2] = 8
                 }  ▼  {
                    [1] = 1,
                    [2] = 8
                 }
It will still pass the inequality statement as true.]]

	for i, v in pairs(codetable) do
		print(attempttable[i], v)
		if attempttable[i] ~= v then
			return print("fail")
		end
	end
	for i, v in pairs(attempttable) do
		print(codetable[i], v)
		if codetable[i] ~= v then
			return print("fail")
		end
	end
	braam:Play()
	return print("successful")
end

--Connects
mouseclick1.MouseClick:Connect(function()
	createnumber(numbers.Number1, puzzle.Joystick1)
end)

mouseclick2.MouseClick:Connect(function()
	createnumber(numbers.Number2, puzzle.Joystick2)
end)

mouseclick3.MouseClick:Connect(function()
	createnumber(numbers.Number3, puzzle.Joystick3)
end)

mouseclick4.MouseClick:Connect(function()
	createnumber(numbers.Number4, puzzle.Joystick4)
end)

mouseclickbutton.MouseClick:Connect(function()
	solvepuzzle()
end)

Sorry, I know this is a lot of code, but any input at all would be greatly appreciated!

Can you show the hierarchy in studio of the way this puzzle is set up? You also didn’t use the variables tblcode or tblattempt at all in your solvepuzzle() function, is there any reason those are there?

1 Like

Apologies for the late response.

Ah, that’s my bad, that’s remnants of me trying to organize the dictionaries using a sorted array, ignore those.

And I have attached a screenshot of the workspace hierarchy.
image

And the dropdown menu for the numbers:
image

image

Not sure if you had anything in mind, but I believe this is now functioning. Probably sloppy, but it works!
Your comment about the hierarchy was very helpful, thank you! I’ll leave this open if you had any additional comments, but don’t feel obligated to.

for i, v in ipairs(codetable) do
		local index = v
		local index2 = attempttable[i]
		for _, k in ipairs(v) do
			if index[2] ~= index2[2] then
				return print("fail")
			end
		end
	end
	braam:Play()
	return print("successful")
end

Have a good rest of your day/night!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.