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

How would you detect a mouse click?

Im trying to found out how would you sense if the MouseButton1 has been clicked anywhere on the screen (not on a GUI). Any help would be appreciated.

(I'm using it for a punching script)

4 comments

0

You could use the player's mouse or UserInputService.

0

How would I do that?

0

Please give me a trustable link with some info on that topicc.

1 answer

✓ Accepted answer

There's 2 ways of doing this, I recommend doing UserInputService. You could also use Player:GetMouse(), but I still recommend using UserInputService.


Player's Mouse

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Down:Connect(function()
	print(mouse.Target, mouse.Hit, mouse.ViewSizeX, mouse.ViewSizeY, mouse.X, mouse.Y)
end)

Mouse.Target and Mouse.Hit is 3D Space and X and Y is screen.


UserInputService

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		print(game.Players.LocalPlayer:GetMouse().Hit) --// And the other things I showed in the other one.
	end
end)

Hope this helped!

1 comment

0

Using UserInputService is redundant in the case of detecting a Button1Down signal, anything in relation with the Mouse should be held within the Mouse. UserInputService is primarily used for bounding functions to keystroke events.

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