How do I check if the distance between 2 GUIS is > a number?

Hello, I am currently working on a game and I want to check if the distance between 2 GUIS is > a number. How do I do that?
Here is what I did but It’s not working:

if (clonedArrow.AbsolutePosition - script.Parent.Main.Right.AbsolutePosition).magnitude < 5 then

end

But It does not work.
Thank you!

2 Likes

Try using .Magnitude, not .magnitude.

You cannot use .magnitude in a GUI because it works only with parts in the workspace.

@NinjaFurfante07 You’re wrong, you can use magnitude with any Vector value, not only parts in the workspace.

Not sure why it doesn’t work, it seems fine to me. Do you get any errors?

Oh, I’m sorry! thanks for letting me know!

1 Like

This would compare the distance between both of the frame centers. Are you looking for the distance between the frame borders?

@polarisprog I checked your script and it is correct you just have to change the value of .magnitude.

1 Like

Maybe the Gui’s AnchorPoint is affecting it?

AbsolutePosition is a read-only property that provides the screen position of a UI element in pixels. This represents the actual pixel position at which an element renders as a result of its ancestors’ sizes and positions. The GuiObject.AnchorPoint also influences the AbsolutePosition.

Guis don’t have a vector value though? Its a Udim2

Nevermind. Absolute position is a Vector2.

Based on the name of the guis, I assume they are making an fnf game (popular rhythm game at the moment.) They would need to check if the arrows that are moving are inside the arrows at the top of their screen.

2 Likes

Their position property is a UDim2 yes, but their AbsolutePosition property is a Vector2 so yes they do actually have it.

1 Like

Congratulations on your cake day!

1 Like

You would have to use math using the pythagorean theorem. This function returns the distance between two guis. This takes the top right corner, but if you needed the closest two corners you would need more math.

local function findDistance(Gui1, Gui2)
      local X = math.abs(Gui1.AbsolutePosition.X - Gui2.AbsolutePosition.X)
      local Y = math.abs(Gui1.AbsolutePosition.Y - Gui2.AbsolutePosition.Y)
     local distance = math.sqrt(X^2 + Y^2)
     return distance
end
1 Like

Never mind I fixed it, I just had to change the 5 to a bigger number and it worked:

if (clonedArrow.AbsolutePosition - script.Parent.Main.Right.AbsolutePosition).magnitude < 100 then

end

Thanks everyone!

hey X^2 means 2 in the power of x…
It should be 2^X

x^2 means x to the power of 2?

yes it is correct . ------------------------------------------