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
8 years ago

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

MyPart = script.Parent


function ColorClick ()
    MyPart.BrickColor = BrickColor.new("Lime green")
end

MyPart.ClickDetector.MouseClick:connect(ColorClick)

if MyPart == "Lime green" then
    function ColorClickTwo ()
        MyPart.BrickColor = BrickColor.new("Really red")

        end
end

MyPart.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 8 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.

--// Functions
function HoverOn(Enter) -- Calls on HoverOn
   script.Parent.BrickColor = BrickColor.new("Lime green")
end

function HoverOff(Leave) -- Calls on HoverOff
   script.Parent.BrickColor = BrickColor.new("Really red")
end

function Click(Click) -- Calls on Click
   print(script.Parent.Name.." was clicked.")
end

--// CallBacks
script.Parent.ClickDetector.MouseHoverEnter:connect(HoverOn)
script.Parent.ClickDetector.MouseHoverLeave:connect(HoverOff)
script.Parent.ClickDetector.MouseClick:connect(Click)

Wish you the best of luck in your journey.

Ad

Answer this question