How to detect if the player's mouse is near the target

I want to code a script that detects if the client mouse is near a target, and I don’t know where to start.

Well first, you would need to get your mouse with

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

Then you would use mouse.Hit.p to calculate the position of the mouse,
and then use magnitude to measure the distance from the mouse to the part / target

1 Like

how do i calculate the position of the mouse, I’m not very good at math

mouse.Hit.p returns the position of the mouse, you could test this with print(mouse.Hit.p)

you should have a look at these resolving anymore questions

that wont work if the target is flying then the mouse position will be too far from the target, even though the mouse is icon is near the target

Use the get mouse function in a local script use mouse.target to get the mouses target use magnitude to decide the distance, i would give a piece of code but im currently not on my pc so ill just out this to tell u how to do this

Also hers a yt vid on on how to use magnitude and the players mouse.
Mouse:

Magnitude:

the tutorial didn’t help me at all :confused:

In a local script, you need to grab the mouse by using

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

After getting the mouse you can get a vector3 that represents its position using
mouse.Hit.p

For the part’s position, you can run something simple as
workspace.Part.Position

You now have 2 positions, Magnitude allows you to easily check the distance using it as
local distance = ( pos - pos ).Magnitude

Rewatch it was many times as u need to its better to learn how to write the code instead of copy and pasting what someone made

It sounds like you’re asking about checking if the mouse is close to an object on the screen, rather than in 3D space, which is what the posts here so far are answering.

The 2D version is not too hard to do.

Use Camera | Roblox Creator Documentation to calculate the position of the object on the screen.

Compare that to the position of the mouse with Magnitude.

For example:

local UserInputService = game:GetService("UserInputService")
local part = workspace.Part
local camera = workspace.CurrentCamera

game:GetService("RunService").Stepped:Connect(function()
	-- Get the on-screen position of an object
	local screenPos, onScreen = camera:WorldToScreenPoint(part.Position)

	if onScreen then
		-- WorldToScreenPoint gives a Vector3 so we should convert it
		local screenPosVector2 = Vector2.new(screenPos.X, screenPos.Y)

		-- Get the mouse position
		local mousePosition = UserInputService:GetMouseLocation()

		-- Compute the distance between the two positions
		local distance = (screenPosVector2 - mousePosition).Magnitude

		-- Check that they're close to each other
		if distance < 100 then
			-- Mouse is close!
			part.BrickColor = BrickColor.Green()
		else
			-- Mouse is not close!
			part.BrickColor = BrickColor.Red()
		end
	end
end)
7 Likes

you mean this?

just use for loop and use that value as distance.
use CFrame.lookAt so you can get the position between mouse direction and your camera
after you get position, just get range between the position and target

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.