Stravant has a ScreenSpace module script which contains multiple functions for interactions between the 2D screen and the 3D environment. I'll give you the function from the module that deals with what you're asking for:
01 | local M = game.Players.LocalPlayer:GetMouse() |
02 | local Cam = game.Workspace.CurrentCamera |
04 | function WorldToScreen(Pos) |
05 | local point = Cam.CoordinateFrame:pointToObjectSpace(Pos) |
06 | local aspectRatio = M.ViewSizeX / M.ViewSizeY |
07 | local hfactor = math.tan(math.rad(Cam.FieldOfView) / 2 ) |
08 | local wfactor = aspectRatio*hfactor |
10 | local x = (point.x/point.z) / -wfactor |
11 | local y = (point.y/point.z) / hfactor |
13 | return Vector 2. new(M.ViewSizeX * ( 0.5 + 0.5 * x), M.ViewSizeY * ( 0.5 + 0.5 * y)) |
16 | local Vec 2 = WorldToScreen(Vector 3. new( 0 , 50 , 0 )) |
18 | Gui.Position = UDim 2. new( 0 , Vec 2. X, 0 , Vec 2. Y) |
Hope this helped!
Note: This only works if it's in a localscript. Also, the Vector2 will be the coordinates of the point from the top left corner of the screen, so if the gui is located in another gui that's offset from the (0, 0), the gui will be positioned incorrectly.