Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Detect if a player is inside of a radius ?

Asked by 3 years ago
Edited 3 years ago

I am trying to make a certain lock on script, where if a humanoid root part is located within a certain distance of the player, on key press, you lock on to his torso, root, etc. I dont understand how this would be accomplished, if you would help, it would be much appreciated!

2 answers

Log in to vote
1
Answered by 3 years ago

If you're talking radius, then height is not a contributing factor. I wrote this function so you could compare two points to get a distance, it includes a third argument that when set to true; will ignore the height difference between the two points while calculating distance.

local function GetDistanceBetween(Point0, Point1, IgnoreHeight)
    local Distance = (Point0 - Point1)
    return (Distance * (IgnoreHeight == true and Vector3.new(1, 0, 1) or Vector3.new(1, 1, 1))).Magnitude
end

-- Below example.

local Distance = GetDistanceBetween(Vector3.new(0, 0, 0), Vector3.new(100, 35, 100), true)
print(Distance)

In the example below the function; ignoring height gives the radius (141.42135620117). If we don't ignore height, then the result would be a bit larger due to the curve caused by the height (145.68801879883).

Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

https://developer.roblox.com/en-us/articles/Magnitude

when key pressed, take the position of the part you want and players humanoidrootpart. then use magnitude to find the distance between the two.

-- example with 2 players
local distance = (humanoidrootpart - humanoidrootpart2).Magnitude 
print(distance)
0
I am trying to achieve a script like this, except it doesnt determine whether the magnitude is in range with a specific part, but with any root part in a radius ssgmalachi 52 — 3y

Answer this question