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

What am I doing wrong?

Asked by
Probix 80
9 years ago

Hello, I'm new to this site and Lua. Okay, lets cut to the chase.

01MyPart = script.Parent
02 
03 
04function ColorClick ()
05    MyPart.BrickColor = BrickColor.new("Lime green")
06end
07 
08MyPart.ClickDetector.MouseClick:connect(ColorClick)
09 
10if MyPart == "Lime green" then
11    function ColorClickTwo ()
12        MyPart.BrickColor = BrickColor.new("Really red")
13 
14        end
15end
16 
17MyPart.ClickDetector.MouseClick:connect(ColorClickTwo)

I am trying to change a red brick to green by clicking it, which works, but now i'm trying to change it back to red by clicking it again. So, i'm basically trying to change the brick color back and forth by clicking it. When I test it, I would click the red brick, and it would turn green. But, when I click it again, it doesn't do anything, and it would say in the output, "attempt to call a nil value".

Sorry if this is a dumb question, but I am new to Lua, and really can't figure this out.

1 answer

Log in to vote
3
Answered by 9 years ago

First off, Welcome to Scripting Helpers. Here is some of the main problems I found. I've realized my first answer didn't work, here is an alternative. This will work like so. If you hover over the part, it will turn green. Once your stop hovering, it will turn red. When you click, in the output will show that is was clicked.

01--// Functions
02function HoverOn(Enter) -- Calls on HoverOn
03   script.Parent.BrickColor = BrickColor.new("Lime green")
04end
05 
06function HoverOff(Leave) -- Calls on HoverOff
07   script.Parent.BrickColor = BrickColor.new("Really red")
08end
09 
10function Click(Click) -- Calls on Click
11   print(script.Parent.Name.." was clicked.")
12end
13 
14--// CallBacks
15script.Parent.ClickDetector.MouseHoverEnter:connect(HoverOn)
16script.Parent.ClickDetector.MouseHoverLeave:connect(HoverOff)
17script.Parent.ClickDetector.MouseClick:connect(Click)

Wish you the best of luck in your journey.

Ad

Answer this question