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

Is there an InputBegan:Connect for only mouse button 1 clicks?

I'm trying to use the user input service to detect when the player has made a left click, this is the code that im using:

local function CheckInput()
	if (UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1)) then
		--blah blah blah
	end
end

UserInputService.InputBegan:Connect(CheckInput)

This works fine with one problem; if the player holds down left click, ANY input on the keyboard will trigger the function. Is there any way to connect to an event for only mouse button 1 clicks? Or just any workaround in general?

1 answer

✓ Accepted answer

If you want UserInputService to detect if the left mouse button is clicked, you would use Enum.UserInputType.MouseButton1

For example.

local UIS = game:GetService("UserInputService")

function inputBegan(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		print("Left Mouse Button Clicked!")
		--Do whatever you want here.
	end
end

UIS.InputBegan:Connect(inputBegan)

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