mouse.Hit.p player detector

Hey all!
I learned the mouse.Hit.p function a short time ago, and I need a little help with using it, how should I detect firstly, if it’s within a specfic range of the player, and secondly, if there’s another player on the location of the mouse?

Thanks for reading(and helping)

1 Like

i specifically use RunService for changes that

there is no OTHER locations only the x and y are there and if you would like range here is so

local Dist = (MousePos - YOUR_OBJECT_THAT_EXISTS.Position).Magnitude

if Dist < YOUR_NUMBER_FOR_DISTANCE then -- can be >,<,=
   -- ...
end
3 Likes

Just a small thing I noticed, I feel like the Distance variable needs a .magnitude at the end?

1 Like

oops sorry forgot to add that :slight_smile:

So basically I should taake the location of the player and compare it to the the location of the mouse.hit.p?

also any idea to detect if there’s a player on the mouse.hit.p location?

just simply use some coding knowledge to do that very easy to do

local Mouse = define mouse

if Mouse.Target:FindFirstChildOfClass("Humanoid") then
   -- ...
end
1 Like

Mouse.Hit.Position is the same as Mouse.Hit.p, I think the second is deprecated. It’s a position of a CFrame since Mouse.Hit is a CFrame. You would subtract the position between the mouse and the player and use Magnitude. Magntiude get’s the length of a vector as stated in the Developer Hub.

local Dist = (MousePos - PLAYER_HUMANOIDROOTPART_POSITION).Magnitude

The above line will subtract the 2 positions and get the magnitude of it. Magnitude won’t get the length of anything except a vector quantity, position is a vector quantity.

More information: Vector3 | Documentation - Roblox Creator Hub

that will get the distance between the parts

That could be done with @FerbZides solution above in the thread, but just saying if you have NPCs in your game too, you should check if the Target is actually a player and not a npc as both of them consist of a Humanoid.

Magnitude get’s the length of a vector, so it’s basically getting the distance between the parts.

so literally, I would do this?

local mouse = game.Players.LocalPlayer:GetMouse()
local Dist = (mouse.Hit.p - [plr.HumanoidRootPart.Pos].Magnitude)

no npc’s in my game, so that’s safe

1 Like

It’s local Dist = (mouse.Hit.p - plr.HumanoidRootPart.Pos).Magnitude

Yes, and then you would compare the magnitude.

so full script would be something like this?

local mouse = game.Players.LocalPlayer:GetMouse()
local Dist = (mouse.hit.p - [location of HRP]).Magnitude
local maxdist = [maxdistance]

if Dist < maxdist then
   if mouse.Target:FindFirstChildOfClass("Humanoid") then
      --effect
   end
end

1 Like

Yeah thats how you will be doing it, except in place of [maxdistance] you actually need a distance in studs.

1 Like

(Plr humanoidrootpart position - mouse position ).Magnitude < 10

– script here
and to get the target its mouse.Target , since u want to get plr >
if game.Players:GetPlayerFromCharacter(Mouse.Target.Parent) then
– its a player
end
// Sorry that I won’t write it in a script atm.
Since u want it to be like to connect if other mouse was there too u should create a instance.new part
at mouse position and if it gets pressed it shows who the part was from.

Make a simple laser gun

The file contains all the details of the video
Make a simple laser gun.rbxl (58.4 KB)

You can try everything the people mentioned here

1 Like