Calculating distance between 2 GUI frames

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 be able to calculate the distance between 2 GUI frames

  2. What is the issue? It keeps returning 0

  3. What solutions have you tried so far? I’ve searched around a bit. My first idea came from a scripting helpers forum post. And that hasn’t worked I’m just sorta stuck now.

function RenderLine()
	for i,lin in pairs(lines) do
		local line = drawLine:Clone()
		local firstVec = lin.FP.AbsolutePosition
		local secVec = lin.SP.AbsolutePosition
		local distance = (firstVec - secVec).magnitude
		print(distance)
	end
end

function spawnLine()
	if lastPoint ~= nil and currentPoint ~= nil then
		table.insert(lines,{FP = lastPoint,SP = currentPoint})
	end
end
2 Likes

How about you just subtract both positions from each other

Maybe this would work.

local Frame1 = script.Parent.Frame1
local Frame2 = script.Parent.Frame2

function CalculateDistance()

local distance = Frame1.AbsolutePosition - Frame2.AbsolutePosition

return distance

end

print(CalculateDistance())
1 Like

Thats what im doing. But it keeps returning 0.

I know what the issue is but i dont know how to fix it. Im setting 2 values, CurrentPoint and LastPoint

But by the time i call the RenderLine() function for some reason its set them to the same frame.

Heres my renderPoints() function

function RenderPoints()
	area:ClearAllChildren()
	for i,pt in pairs(points) do
		local p = point:Clone()
		currentPoint = p
		p.Parent = area
		p.Position = UDim2.new(0,p.AbsoluteSize.X * i + padding * (i - 1),0,area.AbsoluteSize.Y - pt.CAm - p.AbsoluteSize.Y / 2)
		p.ValueText.Text = pt.CAm
		lastPoint = p
	end
end

Just remove magnitude here like that

local distance = firstVec - secVec

Can you be more specific about what you’re trying to do with the distance between those two frames?

Im making a graph and would like to have lines be drawn in between the points.

Can I help you by trying to make a own function for making a frame between those points?

Ok sure I guess if you need i can also send over the project file.

Ok, I have worked this script out so far.

 local Frame1 = script.Parent.Frame1
 local Frame2 = script.Parent.Frame2

function CalculateDistance(Point1, Point2)
	local distance = UDim2.new(0, Frame1.AbsolutePosition.X - Frame2.AbsolutePosition.X, 0, Frame1.AbsolutePosition.Y - Frame2.AbsolutePosition.Y)
	return distance
end

function SetLineGraph()
	local pos = CalculateDistance(Frame1, Frame2)
	local GraphLine = Instance.new("Frame", script.Parent)
	GraphLine.AnchorPoint = Vector2.new(0.5, 0.5)
	GraphLine.Position = UDim2.new(0,(Frame1.AbsolutePosition.X + Frame1.AbsoluteSize.X/2) - pos.X.Offset/2
    ,0,(Frame1.AbsolutePosition.Y + Frame1.AbsoluteSize.Y/2) - pos.Y.Offset/2)
	if pos.X.Offset <= pos.Y.Offset then
		GraphLine.Size = UDim2.new(0,pos.Y.Offset,0,5)
		else if pos.X.Offset > pos.Y.Offset then
			GraphLine.Size = UDim2.new(0,pos.X.Offset,0,5)
		end
	end
end

SetLineGraph()

It simply takes the position of two frames calculates the middle position of it and sets a frame at this position. The rotation isn’t adjusted so you have to do that.

Edit: I overworked that and removed some lines but should work like before.

Ok thank you for the script! :smiley:

I hope it’ll work like you imagine.

Ended up being an issue with how the previous and current positions were being stored.

i changed it up a bit to keep track of the positions of the most recent two dots created and put their positions in variables called: previousPosition and currentPosition

Here is an updated version: GraphTest.rbxl (25.3 KB)

1 Like