How to get a Players mouse position

I just started to try and learn how to get the Players Mouse. I have tried using UserInputService and Player:GetMouse() Which one is more efficient to get the player’s mouse?

1 Like

The built in mouse function is much faster to set up and costs less in terms of performance. However, UserInputService is the brand new version and is much more accurate.

The mouse route is simply this:

local mouse = Player:GetMouse()
mouse.Button1Down:Connect(function()
    local mousePos = mouse.Hit.p
    print(mousePos)
end)

While UserInputService would require RayCasts in order to derive the 3D position. If you’re interested in this approach I can show you some example code!

Hope this helps. :slight_smile:

2 Likes

I would be interested in the UserInputService and RayCasts approach. But when trying to get the Players Mouse is it best to use it in a LocalScript or a regular Script?

When grabbing any input from the player we should use a LocalScript, we can’t grab a player’s input from the server.

local UIS = game:GetService("UserInputService")

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local cam = workspace.CurrentCamera

local MB1 = Enum.UserInputType.MouseButton1 -- left click
local MB2 = Enum.UserInputType.MouseButton2 -- right click
local MB3 = Enum.UserInputType.MouseButton3 -- middle click

local params = RaycastParams.new()
params.FilterDescendantsInstances = {char}
params.FilterType = Enum.RaycastFilterType.Blacklist
params.IgnoreWater = true

local distance = 1000

local function CastRay(x, y)
	local ray = cam:ScreenPointToRay(x, y)
	local result = workspace:Raycast(ray.Origin, ray.Direction*distance, params)
	if result then
		return result.Position, result.Normal, result.Instance, result.Material
	end
end

UIS.InputBegan:Connect(function(input, gp)
	if gp then return end
	if input.UserInputType == MB1 then
		local pos, normal, object, material = CastRay(input.Position.X, input.Position.Y)
		print(pos)
	end
end)

Here is an example script, the UIS (UserInputService) InputBegan event runs whenever the player presses any button. We then check to see if that button was LMB.

If it is, we log the pixel location of the cursor and send it over to the CastRay function. Input.Position gives us the 2D position of course.

Once it arrives at the raycast function we create a ray using cam:ScreenPointToRay(x.y); this simply creates a ray originating from the camera that fires in the direction of the 2D mouse click.

We then assign that ray to a workspace:Raycast() and if we get results we return them.

We can get extra info from this method as well, this is why I prefer this method. Using raycasts means we can find the normal of the 3d click, so if you wanted to make a bullet ricochet at an angle you would easily have the normal to do so.

4 Likes

I’m no expert on Roblox’s internal engine, so you can take what I say with a grain of salt. However, I’m pretty sure raycast are pretty inexpensive. Yes, it does depend on the length of the ray, but just doing one ray a frame isn’t going to cause any significant or noticeable performance decrease.

1 Like