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

Roblox compiler error in a button script? //Very simple//

Asked by 6 years ago

Seriously, I have such simple code. Look.

script.Parent.ClickDetector.MouseClick:connect(print "nice try")

As soon as I start the game, in the console I get "nice try", then I get this error:

Attempt to connect failed: Passed value is not a function

THEN each time I click it, I get this error.

attempt to call a nil value (x5)

Happens if I try to replace that print thing with a function. Yes, it is inside of a part with a clickdetector.

4 answers

Log in to vote
1
Answered by 6 years ago

You're passing the return value of print(), not an actual function. If you wanted to print something when the event is fired then pass a function expression.

script.Parent.ClickDetector.MouseClick:Connect(function()
    print("foo")
end)
Ad
Log in to vote
0
Answered by 6 years ago
function funcname()
print("Nice try")
end

script.Parent.ClickDetector.MouseClick:connect(funcname)
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

This is because you're not calling a function when the MouseClick event happens. But, this is a very simple fix and I can definitely see why you are confused.

function p()
    p("nice try")
end

script.Parent.ClickDetector.MouseClick:connect(p)

That will work because when MouseClick happens, it will call the function 'p'.

FYI, the above code is also the same thing as doing this:

script.Parent.ClickDetector.MouseClick:connect(function()
    print("nice try")
end)

The above is known as an 'anonymous function'.

0
Your first code would cause an infinite recursion Amiaa16 3227 — 6y
0
Is it because the function is called 'print'? Operation_Meme 890 — 6y
0
I fixed that, sorry D: Operation_Meme 890 — 6y
Log in to vote
0
Answered by 6 years ago

You should use a function for something like this.

script.Parent.ClickDetector.MouseClick:Connect(function()
    print("nice try")
end)

This can also be done by this

function nicetry()
    print("nice try")
end

script.Parent.ClickDetector.MouseClick:connect(nicetry)

The function should always be at the top so the script is able to read it before it gets to where you click. if you have it the other way around the script won't know what to do.

Answer this question