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

Help With OnClicked Function?

Asked by
Scootakip 299 Moderation Voter
8 years ago
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?

2 answers

Log in to vote
0
Answered by 8 years ago

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)

Ad
Log in to vote
-1
Answered by 8 years ago

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!

0
Nope. for some reason it still won't work Scootakip 299 — 8y

Answer this question