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

Why will this button's BrickColor not change?

Asked by 9 years ago

When this button is clicked, it will do some code depending on what color it was when it was clicked(I will put the code in later) and then change it's color from green to red, or red to green. But, this script wont work as itis right now.

function onClicked()
    --If the button is green, make it red.
    if script.Parent.BrickColor == "Bright green" then
    script.Parent.BrickColor = BrickColorNew "Bright red"
    --if the button is red, make it green.
    elseif script.Parent.BrickColor == "Bright green" then
        script.Parent.BrickColor = BrickColorNew "Bright red"
    end


end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

3 answers

Log in to vote
3
Answered by 9 years ago

Fixed:

function onClicked()
    --If the button is green, make it red.
    if script.Parent.BrickColor == BrickColor.new("Bright green") then
    script.Parent.BrickColor = BrickColor.new("Bright red")
    --if the button is red, make it green.
    elseif script.Parent.BrickColor == BrickColor.new("Bright green") then
        script.Parent.BrickColor = BrickColor.new("Bright red")
    end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

Never try to compare 2 different data types (you were comparing a BrickColor to a string).

Ad
Log in to vote
1
Answered by
Scubadoo2 115
9 years ago

Two things

1: Your elseif is ALSO checking if it is Bright green, not Bright red like the comment said.

2: Like ezt12 said, you can't compare brick colors to strings, you have to compare them to other brick colors

Just change the script to so:

function onClicked()
    --If the button is green, make it red.
    if script.Parent.BrickColor == BrickColor.new("Bright green") then
    script.Parent.BrickColor = BrickColor.new("Bright red")
    --if the button is red, make it green.
    elseif script.Parent.BrickColor == BrickColor.new("Bright red") then
        script.Parent.BrickColor = BrickColor.new("Bright green")
    end


end

script.Parent.ClickDetector.MouseClick:connect(onClicked)
Log in to vote
0
Answered by 9 years ago

You wrote the BrickColor.new Wrongly, here is the corrected script. Scripts are also case sensitive so the new would also have to be lower case.

function onClicked()
    --If the button is green, make it red.
    if script.Parent.BrickColor == "Bright green" then
    script.Parent.BrickColor = BrickColor.new "Bright red"
    --if the button is red, make it green.
    elseif script.Parent.BrickColor == "Bright green" then
        script.Parent.BrickColor = BrickColor.new "Bright red"
    end


end

script.Parent.ClickDetector.MouseClick:connect(onClicked)


0
Hmm.. Still doesn't work, and I got no output. My_Comment 95 — 9y

Answer this question