Help with Click-Detectors

Hey everyone, I am creating a memory mini game to get better at scripting for now and I am currently stuck on a certain function I need my code to perform.

For this game there are 4 colors to choose from where they all contain Click-Detectors, I am having trouble figuring out how I would be able to tell which one of the Click-Detectors the user has pressed to check if the decision was correct or not. I thought about using ClickDetector.MouseClick:Wait() until I realized that the code wouldn’t proceed until I pressed every color even if I just wanted only one to be pressed to move forward.

Sorry if is this posted poorly. If you need my code to help understand what I have so far to work off of I will gladly provide it.

You can use tables, so say you want the players to press Red Green Blue, you simply make that a table

local CorrectColors = {“Bright red”,“Bright green”,“Bright blue”}

Then whenever a player presses a click detector (I recommend using 1 script for all the click detectors that going make this process a lot easier)

You just record what the players click

local Memory = {} -- [Player] = {Color,...}
EachButton.ClickDetector.MouseClick:Connect(function(Player)
    table.insert(Memory[Player],EachButton.BrickColor.Name)
    if #Memory[Player] == #CorrectColors then
        for i = 1,#CorrectColors do
            if Memory[Player][i] ~= CorrectColors[i] then
                -- Not correct combination break the loop and reset the memory table
                return false
            end
        end
        print("Player has chosen correct combination!")
        return true
    end
end)

-- NOTE: Not designed for copy and paste, adjustments are required
1 Like

Please provide the code you have thus far, I’ll see if anything needs changing/improving, thanks!

I would create a helper function to handle the decision checking. Then connect each ClickDetector to a function that calls the helper function with an argument for the selected colour. Something like this:

local function handleClick(player, selection)
  -- deal with the selection using the arguments
end

redClickConnector.MouseClick:Connect(function(player)
   handleClick(player, "Red")
end)
blueClickConnector.MouseClick:Connect(function(player)
   handleClick(player, "Blue")
end)
1 Like
--[[
*Need to get order of colors
*Need to check if users click order matches with random order chosen
*Add color refresh if the next color is going to be same so user knows
--]]

--Easier access variables for given colors
local blue = script.Parent.Blue
local green = script.Parent.Green
local red = script.Parent.Red
local yellow = script.Parent.Yellow
--To make sure player spam clicking doesn't work
local debounce = false
--Order of the random colors chosen
local colorOrder = {}
--Users color order (The colors they pressed)
local userColorOrder = {}
--Each constant is equal to a second
local speed = .5

function flashColor(randomNum)
	--"randomNum" chooses which color will be next
	if (randomNum == 1) then
		blue.BrickColor = BrickColor.new("White")
		wait(speed)
		blue.BrickColor = BrickColor.new("Bright blue")
	elseif (randomNum == 2) then
		green.BrickColor = BrickColor.new("White")
		wait(speed)
		green.BrickColor = BrickColor.new("Dark green")
	elseif (randomNum == 3) then
		red.BrickColor = BrickColor.new("White")
		wait(speed)
		red.BrickColor = BrickColor.new("Crimson")
	else
		yellow.BrickColor = BrickColor.new("White")
		wait(speed)
		yellow.BrickColor = BrickColor.new("Olive")
	end
end

function userChoice() 
	blue.ClickDetector.MouseClick:Connect(function()
		table.insert(1)
	end)
	green.ClickDetector.MouseClick:Connect(function()
		table.insert(2)
	end)
	red.ClickDetector.MouseClick:Connect(function()
		table.insert(3)
	end)
	yellow.ClickDetector.MouseClick:Connect(function()
		table.insert(4)
	end)
end
	
function startMemoryGame()
	if not debounce then
		debounce = true
		--print("3...")
		--wait(1)
		--print("2...")
		--wait(1)
		--print("1...")
		--wait(1)
		--print("Game Start")
				
		--Iterate through "colorOrder" and make it play the order for the player to see
		--[[
		for index = 1, #colorOrder do
			flashColor(colorOrder[index])
		end
		--]]
		for index, value in ipairs(colorOrder) do
			flashColor(value)
			--If this wait wasn't here, 2 color change codes would happen almost at the same time, i.e. line 29 and line 31
			wait(speed)
		end
		--Assigns "randomNum" a random number from 1-4
		local randomNum = math.random(1,4)
		--Inserts "randomNum" into the array "order"
		table.insert(colorOrder, randomNum)
		flashColor(randomNum)
		
		--**USER CHOICE HERE, USE THE LAST ELEMENT OF "ORDER" TO COMPARE USER CHOICE WITH TO SEE IF THEY GET IT RIGHT
		--*the problem is, i don't know how to check which click detector out of the 4 the user chose...
		--*For testing the array of "colorOrder"
		--print(table.concat(colorOrder))	
		debounce = false
	end
end

--This is really where the code starts, as it is not in any functions and waits for someone to click the button, then the functions start running
script.Parent.Start.ClickDetector.MouseClick:Connect(startMemoryGame)
function userChoice() 
	blue.ClickDetector.MouseClick:Connect(function()
		table.insert(userColorOrder, "blue")
	end)
	green.ClickDetector.MouseClick:Connect(function()
		table.insert(userColorOrder, "green")
	end)
	red.ClickDetector.MouseClick:Connect(function()
		table.insert(userColorOrder, "red")
	end)
	yellow.ClickDetector.MouseClick:Connect(function()
		table.insert(userColorOrder, "yellow")
	end)
end

for _, choice in pairs(userColorOrder) do
	if _ == 1 and choice == "red" then
		--do something
	end
end
1 Like