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).