Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help With OnClicked Function?

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

✓ Accepted answer

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!

1 comment

0

Nope. for some reason it still won't work

This discussion is preserved as it was. New answers, comments, and votes are closed.