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

How do I make a brick color change when a button is clicked?

Asked by 6 years ago

Title explains it all, here's my script.

local isOn = true

function on()
 isOn = true
workspace.lightpart.BrickColor.new(165, 255, 198)
workspace.button.BrickColor.new(165, 255, 198)
end

function off()
 isOn = false
workspace.lightpart.BrickColor.new(255, 114, 114)
workspace.button.BrickColor.new(255, 114, 114)
end

function onClicked()

 if isOn == true then off() else on() end

end

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

on()

I'm a bit of a noob, so please go easy on me!

1 answer

Log in to vote
0
Answered by 6 years ago

First of all, i would like to address the indentation in your functions for future reference, i can see in your version of the script that there is no space between the function and the end. You should always make sure to indent or space until you can clearly see the pair, this makes it easier for others to read your code and edit.

local isOn = true

function on()
    isOn = true
    workspace.lightpart.Color = Color3.new(165, 255, 198)
    workspace.button.Color = Color3.new(165, 255, 198)
end

function off()
 isOn = false
    workspace.lightpart.Color = Color3.new(255, 114, 114)
    workspace.button.Color = Color3.new(255, 114, 114)
end

function onClicked()
     if isOn == true then off() else on() end
end

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

Now the main error in your script is the method you are using to change the color, when changing the property of an object or a variable of any kind you must declare which property/variable and then an equal sign followed by the declaration, which is what you want to change it to. Another issue is you're trying to change a BrickColor, which in nature is a string which refers to a color rather than a color code referring to a color, therefore you must use the instance Color3.new instead of BrickColor.new in order to turn the color into a specific color code.

Ad

Answer this question