Instead of using magnitude lenght is not working

I want use lengh instead of magnitude but it seems not working:

local Part2 = script.Parent
local Part1 = game.Workspace.Part1
local pa
local function Lengh(Vector1, Vector2)
local a = math.sqrt(Vector2*Vector2+Vector1*Vector1)
return a
end
local d = Lengh(Part1, Part2)
print(d)

Error: Workspace.Part2.Script:5: attempt to perform arithmetic (mul) on Instance - Server - Script:5

Did you ever put the positions pf the parts in this script? It seems like you are only putting the parts instance other than the parts position/CFrame.

(Could be wrong, reading this at 1am).

Still not working.

local Part2 = script.Parent
local Part1 = game.Workspace.Part1
local function Lenght(Vector1, Vector2)
local a = math.sqrt(Vector2*Vector2+Vector1*Vector1)
return a
end
local d = Lenght(Part1.Position, Part2.Position)
print(d)

Workspace.Part2.Script:5: invalid argument #1 to ‘sqrt’ (number expected, got Vector3)

Well, yeah, you are putting in coordinates, not numbers.

1 Like

so how can I convert it to numbers?

Not sure how you are doing this exactly, but you can use the axis values (X,Y,Z).

ah let me do it will all components

Why do i get different values bot get the differences

local Part2 = script.Parent
local Part1 = game.Workspace.Part1
local function Lenght(Vector1, Vector2)
local a = Vector1.X^2 + Vector1.Y^2 + Vector1.Z^2
local b = Vector2.X^2 + Vector2.Y^2 + Vector1.Z^2
local result = math.abs(a + b)
return math.sqrt(result)
end
local magnitude = (Part2.Position-Part1.Position).Magnitude
local d = Lenght(Part1.Position, Part2.Position)
print(magnitude)
print(d)

Output:
45
34.792240528791

I am not sure why, I am going to experiment a bit more.

1 Like

You just did the same thing as magnitude but why was our before wrong? I mean we use the pythagrean theorem and sqrt it

I think you are not supposed to use addition when calculate the length:

local function Length(Vector1, Vector2)
	local a = Vector1.X^2 + Vector1.Y^2 + Vector1.Z^2
	local b = Vector2.X^2 + Vector2.Y^2 + Vector1.Z^2
	local result = math.abs(a - b)
	return math.sqrt(result)
end

Oh and additionally, the calculation is in wrong order, perhaps this one is the right one?

local function Length(Vector1, Vector2)
	local x, y, z = (Vector1.X - Vector2.X)^2, (Vector1.Y - Vector2.Y)^2, (Vector1.Z - Vector2.Z)^2
	local result = x + y + z
	return math.sqrt(result)
end
3 Likes

Oh my bad, I thought you were going for the magnitude thing.

Its possible adding the 2 points isn’t the right thing to do.

1 Like

I updated the post above again and apparently when I looked a bit closer into vector math, I remembered that you should calculate the difference between their respective vectors; X for X, Y for Y and Z for Z. Then you can use Pythagorean theorem. Calculating their differences results in a space where either Vector3 becomes the origin in theory.

2 Likes