How to measure distance between two points

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I want to get the exact distance between two points to spwan a rope with exactly that Length
  2. What is the issue? Include screenshots / videos if possible!

    The magnitude doesn’t give me the exact distance
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
local magnitude = (attachment0.Position - attachment1).Magnitude
11 Likes

Magnitude does give the distance, I think you must be inputting the values wrong. However, I may be wrong on that part.

local magnitude = (attachment0.Position - attachment1.Position).magnitude
8 Likes

to

local magnitude = (attachment0.Position - attachment1.Position).Magnitude
4 Likes

Here’s the full script

 local event=game.ReplicatedStorage:WaitForChild("WebSpawn")
local event2=game.ReplicatedStorage:WaitForChild("WebLength")
event.OnServerEvent:Connect(function(player, attachment, pos, canShoot, ropeObj)
	if canShoot.Value==true then --if player can shoot
		canShoot.Value=false
		--setting up the rope
		local rope=Instance.new("RopeConstraint")
		rope.Thickness = 0.2
		rope.Restitution=0.8
		rope.Color = BrickColor.new("White")
		rope.Visible=true;
		rope.Enabled=false
		ropeObj.Value=rope
		local attachment0 = attachment
		local attachment1 = Instance.new("Attachment")
		rope.Attachment0 = attachment0
		rope.Attachment1 = attachment1
		rope.Parent = workspace.Terrain
		attachment1.Parent = workspace.Terrain
		attachment1.Position = pos
		local magnitude = (attachment0.Position - attachment0.Position).Magnitude
		if magnitude<300 then
			rope.Length=magnitude
			wait()
			rope.Enabled=true
			while rope.Length>1 and rope.Enabled do
				wait()
			end
			
		end
		rope:Destroy();
		wait(1)
		canShoot.Value=true
	end
 end)
2 Likes

So I immediately notice you are subtracting attachment0 from attachment0, which would equate to 0. Secondly, if the magnitude is over 300, it will never successfully run the desired code.

4 Likes

Sorry, i made this edit right now and i didn’t noticed it, but i edited it to be attachment1 but still sometimes it gives bigger or smaller distances

1 Like

Did you alter it from attachment1 to attachment1.Position as I posted previously?

2 Likes

Yes at first i used two variables (start and finish) to store the positions and i realized before posting that they are useless, i edited and that’s when i made the mistakes.
now i have like this, but it still doesn’t seem to work

local magnitude = math.abs((attachment0.Position - attachment1.Position).Magnitude)

1 Like
local magnitude = (attachment0.WorldPosition - attachment1.WorldPosition).Magnitude
27 Likes

Thank you very much this one works fine! :slight_smile:

1 Like

the magnitude is never negative, therefore the absolute value function is useless

31 Likes

What if attachment1.Position has a greater value than attachment0.Position?

The magnitude of a vector is the length of that vector. If you think of a vector as an arrow, then the magnitude of the vector is the length of the arrow. Geometrically, it doesn’t make sense for an arrow to have negative length — what would an arrow like that look like? Therefore, the length of an arrow — and likewise the magnitude of a vector — is always positive.

When you subtract two vectors, you get another vector. All vectors have a magnitude and a direction. Regardless of whether the magnitude of attachment1.Position is bigger or smaller than the other attachment, the resulting vector will have its own magnitude which is positive.

Hmm… That makes sense. I wrote this code off the top of my head (but haven’t had the time to test it):

function VectorDistance(a0,a1)
return (a0.WorldPosition.Magnitude - a1.WorldPosition.Magnitude).Magnitude
end