Best Way To Find Distance?

I made a script and two textlabels to detect how far you are from the bluepart and a green part. I’m just wondering if this is the best way to find the distance between blue part and green part. Code:

local player = game.Players.LocalPlayer
local bluePart = game.Workspace.BluePart
local greenPart = game.Workspace.GreenPart
local distanceGreen = player.PlayerGui:WaitForChild("Main").DistanceGreen
local distanceBlue = player.PlayerGui:WaitForChild("Main").DistanceBlue
local findChar = game.Workspace:FindFirstChild(player.Name) -- Finding player's character in workspace
local humanoidRoot = findChar.HumanoidRootPart -- Grabing the player's HumanoidRootPart

while true do
	local playerDistBlue = (humanoidRoot.Position - bluePart.Position).magnitude --[[ Finding the distance between
HumanoidRootPart and bluePart --]]
	distanceBlue.Text = "You are " .. math.floor(playerDistBlue) .. " studs away from the BluePart!"
	local playerDistGreen = (humanoidRoot.Position - greenPart.Position).magnitude  --[[ Finding the distance between
	HumanoidRootPart and bluePart --]]
	distanceGreen.Text = "You are " .. math.floor(playerDistGreen) .. " studs away from the GreenPart!"

Any response it appreciated! :wave:

1 Like

I usually use player:DistanceFromCharacter(position).

1 Like

Your method is fine. I don’t see anything wrong with it.


Actually I might be wrong here, try changing .magnitude to .Magnitude, just in case the first one gets deprecated. Maybe even the lowercase one is slower, I can’t confirm, but I would just recommend changing to .Magnitude

1 Like

Aren’t they the same thing though??

Maybe? I find it weird they both work. Just use .Magnitude for being future proof

1 Like