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
9 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
3 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)
1 Like

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.

3 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
24 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