Maximum Range to Mouse Target/Hit

Hi there, so I’ve been trying to make a Mouse Target Maximum Radius type of system, yet I can’t seem to get anything better than this. So I’ve got the maximum range part when the mouse is hitting a part within the range, but when the mouse is off-range and moving off-range, the model just stays there.

I’m wondering if there’s a function or something to get the furthest CFrame from the character while respecting a maximum radius.

(I don’t know if that was really clear, let me know if it wasn’t and I’ll try making an example of what I’m looking for.)

Here’s the script I’m currently using.
– Setup
local Players = game:GetService(“Players”)
local RunService = game:GetService(“RunService”)
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local LocalPlayer = Players.LocalPlayer

-- Waits for character to load
while LocalPlayer.Character == nil do wait() end

local Character = LocalPlayer.Character
-- Getting a scale character
local PositionPreview = ReplicatedStorage:WaitForChild("Other"):WaitForChild("Scale"):Clone()
PositionPreview.Parent = game.Workspace

local Mouse = LocalPlayer:GetMouse()
Mouse.TargetFilter = PositionPreview

	if (Character.HumanoidRootPart.Position-Mouse.Hit.p).magnitude <= 20 then

So if any of you have an idea on how to do that, make sure to let me know! :slight_smile: Thank you in advance!


Here’s what I would do.
First I’d create a ray from the origin (probably the HumanoidRootPart) to the mouse position. I’d use math.min to pick the minimum value for the mouse distance and the max distance (in your case 20 studs). This minimum value can be used as the distance for the ray. (I recommend reading this devhub article:

Now I can perform a ray cast using workspace:FindPartOnRayWithIgnoreList to achieve the same effects that the player’s mouse object has. Hopefully this is enough information for you to achieve what you want! :smiley:


Thank you! I’ll read that and try to make it work with my system. :slight_smile:

1 Like


I feel that the most feasible way to do this would be to do it with raycasting.

as you have a maximum distance, and an easy way to find the direction, this should be the simplest solution.

local MaxDistance = 20
local origin = Character.HumanoidRootPart.Position
local direction = (Mouse.Hit.Position-origin).Unit
local ray =, direction*MaxDistance)

The above would be an example of how it could look.

If you wanted to apply that to your script, I think it could look something like this:

local hrp =Character.HumanoidRootPart
	if (hrp.Position-Mouse.Hit.Position).magnitude <= 20 then
        local origin = hrp.Position
        local ray =,(Mouse.Hit.Position-origin).Unit*20)
        local p,Pos = workspace:FindPartOnRay(ray, PositionPreview)

Added Notes

Here are some added bits that would be better to use (in my opinion)

  • Using: local Character = plr.Character or plr.CharacterAdded:Wait() would be prefered to using: while LocalPlayer.Character == nil do wait() end

  • Using more variables in your script, such as one for Character.HumanoidRootPart would definitely be easier to digest and type than typing Character.HumanoidRootPart everytime you reference it

Hopefully this helped :smile:


I couldn’t understand how could you use math.min to get the minimum value for the mouse’s distance. How could I get the minimum value, as that dude said: “in your case 20 studs”

I don’t understand how could you make the minimum value using math.min like that.
I’m sorry for asking such an easy thing but can you explain me with a piece of example code?

1 Like

you could use math.clamp() to clamp a value between a minimum and maximum value too, e.g:, min, max), math.clamp(p.Y, min, max), etc.)

This is funnily enough something I forgot existed when I made that reply. math.clamp(value, minimum, maximum) is a lot easier than what I suggested.

@xSa_m You can do something like this:

local minDistance = 0 -- Minimum distance to shoot
local maxDistance = 20 -- Maximum distance to shoot
-- In RenderStepped
local rootPosition = Character.HumanoidRootPart.Position
local difference = rootPosition-Mouse.Hit.p
local distance = math.clamp(difference.Magnitude, minDistance, maxDistance)
local newMouseHit = -(difference.Unit * distance) + rootPosition -- Basically this is making the Magnitude of the difference vector the distance above. Unit vectors have a magnitude of 1, and multiplying a vector by a number multiplies its magnitude, so for example 1 * 20 = 20 magnitude. Then since the difference is rootPosition - mousePosition and we want to replace the mousePosition you can use a little bit of algebra.

How would this be able to deal with collision? ect. there’s a baseplate and the multiplication of the direction by 20 would make it go through it…Yee