Magnitude

magnitude is the lenght of a vector

by AB05S

Author Avatar

What is magnitude? Magnitude is the lenght of a vector. You can find the distance of 2 things with magnitude. You can use magnitude on Vector2 and Vector3 values. I will show you how to use it with both.

Lets start coding!


3D First make two parts and a script. Open your script and write this:

local part = game.Workspace.YourPart  --part
local part2 = game.Workspace.YourPart2 --part2

local pos = part.Position --position
local pos2 = part2.Position --position

local magnitude = (pos - pos2).Magnitude --distance between them

print(magnitude) --printing the distance

You substract the both positions and add .Magnitude


2D You have to use AbsolutePosition. First make a screengui. Then make two guis and a localscript. Make the parent of the localscript the screengui (dont put it in a gui else it wont work). Open your script and write this:

local Gui = script.Parent.YourGui --Gui
local Gui2 = script.Parent.YourGui2 --Gui2

local pos = Gui.AbsolutePosition --Gui position
local pos2 = Gui2.AbsolutePosition -Gui position

local magnitude = (pos - pos2).Magnitude --distance between them

print(magnitude) --printing the distance

I have tested this code and it worked.

Article about Magnitude:

https://developer.roblox.com/en-us/articles/Magnitude

Article about AbsolutePosition:

https://developer.roblox.com/en-us/api-reference/property/GuiBase2d/AbsolutePosition

View in-game to comment, award, and more!