Get the center of a frame

How can I get the center of a Frame with Vector2?

2 Likes

Here’s some example code:

--Gets the center of the frame as a UDim2
local scaleX = frame.Position.X.Scale + frame.Size.X.Scale/2
local scaleY = frame.Position.Y.Scale + frame.Size.Y.Scale/2
local offsetX = frame.Position.X.Offset + frame.Size.X.Offset/2
local offsetY = frame.Position.Y.Offset + frame.Size.Y.Offset/2
local centerUDim2 = UDim2.new(scaleX, offsetX, scaleY, offsetY)

--Gets the center of the frame as a Vector2 (pixel position)
local center = frame.AbsolutePosition + frame.AbsoluteSize/2
2 Likes

Use scale 0.5, 0.5. I’m guessing you mean UDim2?

I mean Vector2 not UDim2…

If I do Vector2.new(gui.Frame.AbsoluteSize.X / 1.7,gui.Frame.AbsoluteSize.Y / 1.7)
it works but if I change the size of the part (because I’m working on a surface gui) it will not work

More details need to be provided to help understand how it’s not working. What specifically are you doing? How do things look? Are there any errors posted to the console? Where you applying this Vector2 or how are you using it in your code?

Please be sure to explain your scenario and provide as much information as possible so we can further help you determine a source of error.

I’m making a fe radar on a part. I need the center position of the gui surface

This response isn’t sufficient. Please refer to my post. You need to add a detailed explanation for your use case and the problems you’re encountering. Can’t do much with a brief line of code and only receiving the reply “it doesn’t work”.

SurfaceGui has a CanvasSize property, which is a Vector2 and corresponds to the size in canvas pixels of the SurfaceGui. The center of that is simply the CanvasSize divided by 2.