Why does this if statement not reveal the frame

  1. What do you want to achieve? Keep it simple and clear!
    I want to check an if statement and when all the squares all blue the button reveals itself
  2. What is the issue? Include screenshots / videos if possible!
    robloxapp-20210215-1632418.wmv (638.3 KB)

    The issue is when all the squares are blue and when I click the done nothing happens and the button does not reveal its self.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried looking for typos in my code and I have not found anything wrong and it does not give any error in the output. I’m wondering if there is something wrong with the if statement.
script.Parent.MouseButton1Click:Connect(function()
	if game.Players.LocalPlayer.PlayerGui.PC.ScreenGui.Puzzle_4mainFrame.Square_Puzzle.Frame_3.BackgroundColor3 == Color3.new(0, 0.333333, 1) 
		and game.Players.LocalPlayer.PlayerGui.PC.ScreenGui.Puzzle_4mainFrame.Square_Puzzle.Frame_4.BackgroundColor3 == Color3.new(0, 0.333333, 1) 
		and game.Players.LocalPlayer.PlayerGui.PC.ScreenGui.Puzzle_4mainFrame.Square_Puzzle.Frame_5.BackgroundColor3 == Color3.new(0, 0.333333, 1) 
		and game.Players.LocalPlayer.PlayerGui.PC.ScreenGui.Puzzle_4mainFrame.Square_Puzzle.Frame_6.BackgroundColor3 == Color3.new(0, 0.333333, 1)
and game.Players.LocalPlayer.PlayerGui.PC.ScreenGui.Puzzle_4mainFrame.Square_Puzzle.Frame_7.BackgroundColor3 == Color3.new(0, 0.333333, 1)
 and game.Players.LocalPlayer.PlayerGui.PC.ScreenGui.Puzzle_4mainFrame.Square_Puzzle.Frame_8.BackgroundColor3 == Color3.new(0, 0.333333, 1)
		and game.Players.LocalPlayer.PlayerGui.PC.ScreenGui.Puzzle_4mainFrame.Square_Puzzle.Frame_9.BackgroundColor3 == Color3.new(0, 0.333333, 1) then
		game.Players.LocalPlayer.PlayerGui.PC.ScreenGui.Puzzle_4mainFrame.Next_tofinalpuzzle.Visible = true
		
	end  
		
	 
	end
		
	)
1 Like

Let’s clean up your code a bit so I can read it:

local puzzle = game.Players.LocalPlayer.PlayerGui.PC.ScreenGui.Puzzle_4mainFrame.Square_Puzzle

script.Parent.MouseButton1Click:Connect(function()
    if puzzle.Frame_3.BackgroundColor3 == Color3.new(0, 0.333333, 1) and
        puzzle.Frame_4.BackgroundColor3 == Color3.new(0, 0.333333, 1) and
        puzzle.Frame_5.BackgroundColor3 == Color3.new(0, 0.333333, 1) and
        puzzle.Frame_6.BackgroundColor3 == Color3.new(0, 0.333333, 1) and
        puzzle.Frame_7.BackgroundColor3 == Color3.new(0, 0.333333, 1) and
        puzzle.Frame_8.BackgroundColor3 == Color3.new(0, 0.333333, 1) and
        puzzle.Frame_9.BackgroundColor3 == Color3.new(0, 0.333333, 1) then
        puzzle.Next_tofinalpuzzle.Visible = true
    end
end)

So you can’t compare colors with ==.

Also, you probably shouldn’t be comparing colors in the first place.

What is the code that’s causing the colors to change? You can use the same code to change to the next puzzle.

Can we see your explorer? I guarantee you there is an easier way to change all the colors at once using a for loop instead of going one by one.

okay so how can I compare the colors? and here is the code that changes the colors ```lua

	game.Players.LocalPlayer.PlayerGui.PC.ScreenGui.Puzzle_4mainFrame.Square_Puzzle.Frame_3.BackgroundColor3 = Color3.new(0, 0.333333, 1)
end)
--Also I have more scripts that change the color for each block

You shouldn’t compare colors to determine if a puzzle if completed, but if you insist:

local function AreColorsSimilar(c1, c2)
    return math.abs(c1.R - c2.R) < 0.001
       and math.abs(c1.G - c2.G) < 0.001
       and math.abs(c1.B - c1.B) < 0.001
end

use like:

if AreColorsSimilar(puzzle.Frame_3.BackgroundColor3, Color3.new(0, 0.333333, 1)) then

I’m confused about the code you just wrote right there are you selecting values from RGB instead of color3 values. Also, what are you using math.abs for?

Also im not changing the colors im comparing them in a if statement and if all conditions meet the statement it makes the frame visible.

Color3s have R, G, and B values (red, green, and blue).

I am comparing each component one-by-one.

The math.abs part is a way to check if two numbers are close to one another. You subtract them and then take the absolute value.

So like 3.999999 and 4.000001 - the difference is -0.000002. math.abs makes that 0.000002. Which is less than 0.001.

1 Like

I will check to see if I have any success with your method of comparing the values.

this is spaghetti code I really suggest changing it, possibly just doing the check of all the children that are a frame

1 Like

I know but that was not the original problem. The problem is why is the if statement not working.

Wait I just realized wouldn’t that result in the same the if statement above.Also you said there was an easier way to detect if the puzzle was finished.How I would actually detect that?

I mean, don’t determine the state of the puzzle from colors in the first place.

Take a step back and do some designing!

Store the puzzle as a list of numbers or something, and base the colors off of those.

Here’s a simple example. Put four buttons Button1, Button2, Button3, and Button4 inside a ScreenGui with this LocalScript.

It’s not exactly the same as your puzzle (because you didn’t really give any code!), but see if you can try and learn from it.

local buttons = {
	script.Parent.Button1,
	script.Parent.Button2,
	script.Parent.Button3,
	script.Parent.Button4
}

-- have one of these for each button
-- doesn't need to be number of times clicked, it could be a number or a string
-- or whatever you want to represent the state of a button.
local numberOfTimesClicked = {
	0,
	0,
	0,
	0
}

-- updates all the button colors based on the numberOfTimesClicked table
local function UpdateColors()
	for i = 1, #numberOfTimesClicked do
		local numClicked = numberOfTimesClicked[i]

		if numClicked == 0 then
			buttons[i].BackgroundColor3 = Color3.new(1, 1, 1)
		elseif numClicked == 1 then
			buttons[i].BackgroundColor3 = Color3.new(1, 0, 0)
		elseif numClicked == 2 then
			buttons[i].BackgroundColor3 = Color3.new(0, 1, 0)
		elseif numClicked == 3 then
			buttons[i].BackgroundColor3 = Color3.new(0, 0, 1)
		elseif numClicked >= 4 then
			buttons[i].BackgroundColor3 = Color3.new(0, 0, 0)
		end
	end
end

-- resets everything when all buttons have been clicked enough times
local function CheckPuzzle()
	local isDone = true

	-- check if everything has been clicked at least 4 times
	for i = 1, #numberOfTimesClicked do
		if numberOfTimesClicked[i] < 4 then
			isDone = false
			break
		end
	end

	-- reset everything
	if isDone then
		print("PUZZLE COMPLETE!")
		for i = 1, #numberOfTimesClicked do
			numberOfTimesClicked[i] = 0
		end
	end

	-- since colors are based off of numberOfTimesClicked, we can just call this and it will set all the colors correctly
	UpdateColors()
end

-- each button just adds 1 to its associated numberOfTimesClicked
for i, button in pairs(buttons) do
	button.MouseButton1Down:Connect(function()
		numberOfTimesClicked[i] += 1 -- add 1 (or update your state however)
		
		print(button.Name .. " clicked a total of " .. tostring(numberOfTimesClicked[i]) .. " times!")

		UpdateColors() -- update colors to pick up the above change
		CheckPuzzle() -- see if we're done and maybe reset the puzzle
	end)
end

The most important methods to look at are UpdateColors and the loop at the bottom that hooks up the clicks.

Note how I don’t compare colors every, I just compare numberOfTimesClicked.

You might not want to keep track of numberOfTimesClicked, maybe you want to keep track of currentColor and it toggles between "white", "blue", "green" or something.

Handle updating adjacent neighbors inside the MouseButton1Down connection by changing that state table (numberOfTimesClicked or currentColor or whatever).

1 Like

Thank you for the guide I have learned what I need to do to check when the puzzles is finished.