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

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

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

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)
function funcname()
print("Nice try")
end

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

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'.

3 comments

0

Your first code would cause an infinite recursion

0

Is it because the function is called 'print'?

0

I fixed that, sorry D:

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.

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