Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you find the magnitude between two frames inside an ScreenGui?

Using ROBLOX's WIKI page for magnitude I manged to make it work for bricks, now trying to find the magnitude between frames is not working as well :

WIKI : (http://wiki.roblox.com/index.php?title=Magnitude)

WORKING CODE :

local center = game.Workspace.center
local short = game.Workspace.short
local long = game.Workspace.long
local magnitude = (center.Position - short.Position).magnitude
print(magnitude)

Now the working code is ONLY for the bricks, but I am trying to make this move over to a ScreenGUI + ImageButton.

Would I need to make my own Vector3 values in order to get the position, because using [(frame.Position - frame2.Position).magnitude] gives me an error

This leads me to that fact I need to get in individual values of the UDim2 properties, which I don't know how to do and that's why I came here.

ERROR : magnitude is not a valid member, did you forget to capitalize the first letter? 13:30:37.945 - Script 'Players.Player1.PlayerGui.ScreenGui.ImageButton.Script', Line 5

1 answer

✓ Accepted answer

You can use the Pythagorean theorem here. Simply plug in the X and Y values for the two objects .

dist = sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2)

This formula only works if your parts are positioned using the Offset property.

local center = game.Workspace.center
local short = game.Workspace.short
local long = game.Workspace.long

-- dist = sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2)

local magnitude = math.sqrt(((center.Position.X.Offset - short.Position.X.Offset) ^ 2) + ((center.Position.Y.Offset - short.Position.Y.Offset) ^ 2))
print(magnitude)

6 comments

0

Do you mind giving me a filled in example, I am not very good at math, my apologies.

0

Wil do - gimme a minute or so

0

No problem, take your time. Thank you.

0

Did this end up helping you?

0

Thanks!

0

It sure did!

This discussion is preserved as it was. New answers, comments, and votes are closed.