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

How to detect if a player presses a button?

Asked by 4 years ago
Edited 4 years ago

I am having trouble with my script

script.Parent.MouseButton1Click:Connect(function)
    print(“Click”)
end)
0
Your code is right, maybe the script is in the wrong place. Where is your script? royaltoe 5144 — 4y

3 answers

Log in to vote
1
Answered by 4 years ago

You use (function()


script.Parent.MouseButton1Click:Connect(function() print(“Click”) end)
0
you just copy pasta'd the code in his question smh royaltoe 5144 — 4y
0
he forgot the two parentheses after function you silly goose alivemaeonman 14 — 4y
0
lol Slatryte 104 — 4y
0
actually i wrote it off memory EnzoTDZ_YT 275 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by
Slatryte 104
4 years ago

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

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

Answer this question