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

How to detect if a player presses a button?

I am having trouble with my script

script.Parent.MouseButton1Click:Connect(function)
    print(“Click”)
end)

1 comment

0

Your code is right, maybe the script is in the wrong place. Where is your script?

3 answers

You use (function()


script.Parent.MouseButton1Click:Connect(function()
    print(“Click”)
end)

4 comments

0

you just copy pasta'd the code in his question smh

0

he forgot the two parentheses after function you silly goose

0

lol

0

actually i wrote it off memory

It depends on where you have your script.

If you have your script inside of the TextButton or ImageButton, then it should work. Just fix your first line to be this:

script.Parent.MouseButton1Click:Connect(function() --// Script is inside of TextButton or ImageButton.
	--// Code to be fired when the button is clicked
end)

If it's not inside of it, you would have to reference the TextButton or ImageButton, otherwise it won't work. No other GUI element has the ability to be clicked and react as a function. So you would have to do this:

script.Parent.TextButton.MouseButton1Click:Connect(function() --// Change this to the location of where the button is located at. This script is not inside of the TextButton or ImageButton.
	--// Code to be fired when the button is clicked
end)

It really all depends on how you have your GUI and Scripts setup.

Hope this helped! If you have anymore questions comment down below and I'll try to help you out as much as I can! If this worked, select this as the answer so everyone else knows this question has been solved. If it didn't work, comment down below what went wrong with a picture of how you have your GUI setup so it will be easier to figure this out. Glad to be of help!

~killerbrenden

You put the function inside the brackets! That is why! This should work:

function onClicked()
print("Click")
end
script.Parent.MouseButton1Click:Connect(onClicked)

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