But = script.Parent.Parent Button = script.Parent But.Name = "Fan On" Button.BrickColor = BrickColor.new("Really blue") function oc(PWC) if But.Name == "Fan On" then But.Name = "Fan Off" Button.BrickColor = BrickColor.new("Really red") if But.Name == "Fan Off" then But.Name = "Fan On" Button.BrickColor = BrickColor.new("Really blue") end end end script.Parent.ClickDetector.MouseClick:connect(oc)
First time using OnClicked. I followed what the Roblox Wiki told me to do for basic onClick functions and I added my own code as well, but it doesn't work. Can someone help?
The problem looked to be that you had your off if statement inside of your on. So the only time it would even check if it was off, is if it was on in the first place. Otherwise after that it wouldn't do anything because it's on by default and could never change.
But = script.Parent.Parent Button = script.Parent But.Name = "Fan On" Button.BrickColor = BrickColor.new("Really blue") function oc(PWC) if But.Name == "Fan On" then But.Name = "Fan Off" Button.BrickColor = BrickColor.new("Really red") elseif But.Name == "Fan Off" then But.Name = "Fan On" Button.BrickColor = BrickColor.new("Really blue") end end script.Parent.ClickDetector.MouseClick:connect(oc)
This should work:
But = script.Parent.Parent Button = script.Parent But.Name = "Fan On" Button.BrickColor = BrickColor.new("Really blue") function oc(PWC) if But.Name == "Fan On" then But.Name = "Fan Off" end Button.BrickColor = BrickColor.new("Really red") if But.Name == "Fan Off" then But.Name = "Fan On" Button.BrickColor = BrickColor.new("Really blue") end end script.Parent.ClickDetector.MouseClick:connect(oc)
For your first "if" statement, you forgot to close it. Hope this helps!