When you click, button turns green or red

Here’s the code

local green = (script.Parent.Color == Color3.new(0.607843, 1, 0.490196))
            local  red = (script.Parent.Color == Color3.new (1, 0.341176, 0.341176))

        local ClickDetector = game.Workspace.Button.ClickDetector

        function onMouseClick()
        	wait(0.1)
        	local RandomNumber = math.random(1,2)

        	if  RandomNumber == 1 then
        		function green ()

        		end

        	end





        	if RandomNumber == 2 then 

        		function red ()

        		end
        	end

        end
        	
        ClickDetector.MouseClick:connect (onMouseClick())

Why isn’t the code working?

The code wouldn’t work because a few things,

When you were connecting the function to the button, you were calling it because you put the ()

Your functions for chnaging the color is empty so it wouldn’t work even if you did that fixt

Put t he green and red colors in variables and just reference those instead

local green = Color3.new(0.607843, 1, 0.490196)
local red = Color3.new(1, 0.341176, 0.341176)

local ClickDetector = game.Workspace.Button.ClickDetector

function onMouseClick()
	wait(0.1)
	local RandomNumber = math.random(1,2)

	if RandomNumber == 1 then
		ClickDetector.Parent.Color = green
	else
		ClickDetector.Parent.Color = red
	end
end
    	
ClickDetector.MouseClick:Connect(onMouseClick)

Is how you’d do it, when button is pressed, activate that function, get a random number, and if the number is 1, change the color to green, else red

You alternative for the color changing you can do ClickDetector.Parent.Color = RandomNumber == 1 and green or red instead of that if statement

1 Like

That’s more easier to read, and more simpler. Can’t believe I didn’t think of that… Anyways it works, thank you so much!

1 Like

Anytime! If you have anymore issues don’t be afraid to make another post!