Getting Center of Frame Not Working

Im getting the center of a frame using Vector2 and then converting it to a Udim2, but what I am trying to position is way off

function self.getCenter(object)
	local center = Vector2.new(object.AbsolutePosition.X, object.AbsolutePosition.Y) + object.AbsoluteSize / 2
	return UDim2.fromScale(center.X,center.Y)
end

That part works just fine. The problem must be with the rest of the code. Can we see the rest of the script?

Shouldn’t it be minus half of the absolute size rather than adding it?

1 Like

No it should be plus, because the anchor point starts at 0 pixels by 0 pixels. To get the center you need to add half the X and Y.

Ok, I think the reason is because you are creating a UDim2 that uses the absolute position as the scale parameter instead of offset.

function getCenter(object)
    local center = Vector2.new(object.AbsolutePosition.X, object.AbsolutePosition.Y) + object.AbsoluteSize / 2
    return UDim2.fromOffset(center.X,center.Y)
end
1 Like

Alright it worked could you explain why this worked though?

Because the object Im passing through is in scale

The scale value in UDim2 is usually only between values 0 and 1, which are basically the edges of the element(in this case, your screen). The absolute position of a GUI is the position of it according to pixels, counting from the top left of your screen.
Here’s an example: You want to get the center of a 1080p screen.
With scale, you would do UDim2.fromScale(0.5, 0.5), because the number 0.5 is in the middle of 0 and 1.
However, with absolute position, it would be something like UDim2.fromOffset(1920/2, 1080/2), because dividing the screen’s dimensions by 2 gives you the middle.
You can notice the drastic difference between scale and absolute position. And because you used fromScale with absolute position, it won’t work.

1 Like

Oh I get what you mean, thanks for helping me and taking the time to explain it to me aswell :tongue:

1 Like