I am wondering how I would find the real-time in-game distance between a part and a humanoidrootpart. I’ve made a beam system that creates a line between the player and a part, but I want to make it to where the beam disappears when the player is outside of a certain range. For example I want to be able to find is player >x(distance) from part then beam.visible = false and blah blah blah. What would I use to find this?
First find the displacement between the part and the HumanoidRootPart. The displacement would be the vector that when added to either one, will arrive at the other position.
local disp: Vector3 = HumanoidRootPart.Position - Part.Position
Then, you get the length of the displacement by simply doing
disp.Magnitude, which is just the Pythagorean theorem in 3D.
If you want to compare distances however, it might be more efficient to avoid using magnitude since that involves using squareroots which are pretty expensive. Observe:
if a.Magnitude > b.Magnitude then
--is the same as
if math.sqrt(a.X^2 + a.Y^2 + a.Z^2) > math.sqrt(b.X^2 + b.Y^2 + b.Z^2) then
--algebra tells you that whatever you do, you must do to both sides to remain equal
--so we will remove the squareroot from both sides
--now it becomes
if a.X^2 + a.Y^2 + a.Z^2 > b.X^2 + b.Y^2 + b.Z^2 then
So for your beam, you will just constantly check that magnitude and whenever it exceeds a set value you make it disappear
local maxRange: number = 100
Beam.Enabled = (HumanoidRootPart.Position - Part.Position).Magnitude <= maxRange
using the .Magnitude function of a vector to get the length of it. take the HumanoidRootPart position you want and the part position you want, subtract one from another and then use the Magnitude function on it to get the distance.
local rootPart = character.HumanoidRootPart
local part = workspace.Part
local distance = (rootPart.Position - part.Position).Magnitude
Thank you for the great response!
I implemented this method into my game, however I think the way I did it was incorrect as it isn’t working. Here is the snippet of code that mostly controls the beam. There’s other code but it is just determining which tycoon (it’s for a tycoon game) is the players which is therefore where the beam will come from, which works fine.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local function CreatePart(character, objectToFollow)
if not workspace.CurrentCamera:FindFirstChild(objectToFollow.Parent.Name) then
local newPart = script.Part:Clone()
newPart.Name = objectToFollow.Parent.Name
newPart.Anchored = true
newPart.CanCollide = false
newPart.Material = Enum.Material.ForceField
newPart.CanTouch = false
newPart.Color = script.ColorValue.Value
newPart.Parent = workspace.CurrentCamera
if newPart then
local function FindFunction() -- where I started implemeting code that controls part position
local Mid = (character.HumanoidRootPart.Position + objectToFollow.Position)/2
local dist = (character.HumanoidRootPart.Position - objectToFollow.Position).Magnitude
newPart.CFrame = CFrame.lookAt(character.HumanoidRootPart.Position, objectToFollow.Position)
newPart.Position = Mid
newPart.Size = Vector3.new(script.SizeX.Value, script.SizeY.Value, dist)
if dist > 50 then -- where I detect distance/ likely where problem is
newPart.Transparency = 0
newPart.Transparency = 1
local LoadCor = coroutine.create(function()
while objectToFollow.Parent ~= nil and objectToFollow.Transparency == 0 and character ~= nil do
I don’t know why it’s not working as I don’t have much experience in this certain field yet, obviously
Please describe the problem. If it isn’t working, is it that the beam never appears or that it appears at the wrong time and place?
What you have currently means that the beam will appear when the distance exceeds 50 studs.
LOL that solved the problem . Thank you for pointing that out it works perfectly now thank you so much!
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.