Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a button that changes color once I click it and reverts back when I click it again?

Asked by 5 years ago
Edited 5 years ago

I want to make a button that changes color once I click it and reverts back to normal once I click it again. I made a script, it does not work. Here is the script.

            function leftClick()
    script.Parent.TextColor3 = Color3.new(0, 255, 0)
end
        function leftClick()
    script.Parent.TextColor3 = Color3.FromRGB(255,0,0)
end

script.Parent.MouseButton1Click:Connect(leftClick)

2 answers

Log in to vote
0
Answered by
srimmbow 241 Moderation Voter
5 years ago
Edited 5 years ago
local on = false -- Set it to false if it's off when the game starts (on is true)

local function leftClick()
    if not on then
        script.Parent.TextColor3 = Color3.fromRGB(255, 0, 0)
        on = true
    else
        script.Parent.TextColor3 = Color3.fromRGB(0, 255, 0)
        on = false
    end
end

script.Parent.MouseButton1Click:Connect(leftClick)
0
There was a mistake on your script, I fixed it. You forgot to make local on = true, if not not on. GamingWithFlight 80 — 5y
0
oops! yeah, sorry about that srimmbow 241 — 5y
0
I fixed the script srimmbow 241 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

**Is the button a GUI or a part of the workspace? **

*You need to use a click detector. Put one into you object. *

ClickDetector.MouseClick:Connect(function()
    --Write code here to change colors.
end)
0
GUI Button, not the surface label. GamingWithFlight 80 — 5y
0
I think he is talking about a GUI button because the event is MouseButton1Click which is for Gui buttons srimmbow 241 — 5y

Answer this question