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

How do you determine what button the mouse clicked?

Hello,

UserInputService.InputBegan:connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then

I am trying to write if the mouse has clicked a button called ("button1") then but I am unsure on how to write it in terms of lua.

5 comments

0

You could just use a ClickDetector if your button is a part. Or get the Event MouseButton1Click or Activated if it's a GUIButton. No need to get the mouse

0

Yes. I was going to do it script.Parent.MouseButton1Click:connect(function() instead but I've been told that userinputservice is always better. I don't know how it could work for this application though.

0

No if you were to click a button, like a GUI then I would say you should use MouseButton1Click or Activated

0

Okay, will do. Thank you

0

No problem

1 answer

Use MouseButton1Click when trying to detect left-click on a GUI. Use MouseButton2Click when trying to detect right-click on a GUI.

For example:

--For local script under a TextButton.
script.Parent.MouseButton1Click:Connect(function()
	print("Parent was clicked.")
end)

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