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 every time you click?

Asked by 6 years ago

I made button where you press brick and then it shows surface box, but when you click it again it doesn't show it anymore. Here's my script:

local isOn = false

function onClicked() 
    isOn = true
    script.Parent.SelectionBox.Visible = true
    script.Parent.SelectionBox.Color3 = Color3.new(255,0,0)
end
function offClicked()
    isOn = false
    script.Parent.SelectionBox.Visible = false
    script.Parent.SelectionBox.Color3 = Color3.new(0,0,0)
end
    script.Parent.ClickDetector.MouseClick:connect(onClicked)

    offClicked()

For some reason, when I click it, it shows everything I need but when I click it again, it doesn't turn off like I want it too. Why?

1 answer

Log in to vote
0
Answered by
Vulkarin 581 Moderation Voter
6 years ago

You have the event ClickDetector.MouseClick connected to the function onClicked but no event connected to function offClicked

You could maybe combine them with your "isOn" boolean value

local isOn = false

function onClicked()
    if not isOn then
        isOn = true
        script.Parent.SelectionBox.Visible = true
        script.Parent.SelectionBox.Color3 = Color3.new(255,0,0)
    else
        isOn = false
        script.Parent.SelectionBox.Visible = false
        script.Parent.SelectionBox.Color3 = Color3.new(0,0,0)
    end
end

script.Parent.ClickDetector.MouseClick:Connect(onClicked)
Ad

Answer this question