I’m trying to get the position of the corner of a textbutton
I have tried the formula I know to get the edge of part
local x1 = Frame.Position.X.Scale+Frame.Size.X.Scale/2
local x2 = Frame.Position.X.Scale-Frame.Size.X.Scale/2
local y1 = Frame.Position.Y.Scale+Frame.Size.Y.Scale/2
local y2 = Frame.Position.Y.Scale-Frame.Size.Y.Scale/2
local corner1 = UDim2.fromScale(x1, y1)
local corner2 = UDim2.fromScale(x1, y2)
local corner3 = UDim2.fromScale(x2, y1)
local corner4 = UDim2.fromScale(x2, y2)
But when I create new frames on those positions, they are in the incorrect places
instead of using Frame.Size.X.Scale and Frame.Size.Y.Scale, you need to use Frame.Size.X.Offset and Frame.Size.Y.Offset. This is because Frame.Size.X.Offset and Frame.Size.Y.Offset give us the size of the frame in pixels, while Frame.Size.X.Scale and Frame.Size.Y.Scale give us the size of the frame as a percentage of its parent’s size.
local x1 = Frame.Position.X.Scale + Frame.Position.X.Offset / Frame.AbsoluteSize.X
local y1 = Frame.Position.Y.Scale + Frame.Position.Y.Offset / Frame.AbsoluteSize.Y
local x2 = (Frame.Position.X.Scale + Frame.Size.X.Scale) + Frame.Size.X.Offset / Frame.AbsoluteSize.X
local y2 = (Frame.Position.Y.Scale + Frame.Size.Y.Scale) + Frame.Size.Y.Offset / Frame.AbsoluteSize.Y
local corner1 = UDim2.new(x1, 0, y1, 0)
local corner2 = UDim2.new(x1, 0, y2, 0)
local corner3 = UDim2.new(x2, 0, y1, 0)
local corner4 = UDim2.new(x2, 0, y2, 0)
Also, note that in the UDim2.new calls, we’re setting the second argument to 0 instead of y1 or y2. This is because UDim2.new expects a scale and an offset, and we’re only interested in the scale component here.
Since AnchorPoint is 0.5, 0.5 it’ll be better like this. Here’s your code:
local function getCorners(guiObject: GuiObject): {UDim2}
local position = guiObject.AbsolutePosition
local size = guiObject.AbsoluteSize
return {
UDim2.fromOffset(position.X, position.Y),
UDim2.fromOffset(position.X + size.X, position.Y),
UDim2.fromOffset(position.X + size.X, position.Y + size.Y),
UDim2.fromOffset(position.X, position.Y + size.Y)
}
end
local function scale(corners: {UDim2}, parentSize: Vector2): {UDim2}
for i, v in pairs(corners) do
local corner = corners[i]
corners[i] = UDim2.fromScale(
corner.X.Offset / parentSize.X,
corner.Y.Offset / parentSize.Y
)
end
return corners
end
Parent size in scale function is a Vector2 so that you can pass CurrentCamera.ViewportSize if the parent isn’t a GuiObject! If it is tho, pass GuiObject.AbsoluteSize.
Nvm I didn’t need to, I found out why, when calculating the corners, I’m calculating it in a way that the frames will be children of a ScreenGui and not the parent, to fix it, change the getCorners to this:
local function getCorners(guiObject: GuiObject): {UDim2}
local position = guiObject.AbsolutePosition
local size = guiObject.AbsoluteSize
return {
UDim2.fromOffset(0, 0),
UDim2.fromOffset(size.X, 0),
UDim2.fromOffset(size.X, size.Y),
UDim2.fromOffset(0, size.Y)
}
end
@hbgnhu, created a better method, now you can parent the frame to (I think) anything and it should work fine regardless of it’s properties, here are the functions.
local function getCorners(guiObject: GuiObject): {UDim2}
local position = guiObject.AbsolutePosition
local size = guiObject.AbsoluteSize
return {
UDim2.fromOffset(position.X, position.Y),
UDim2.fromOffset(position.X + size.X, position.Y),
UDim2.fromOffset(position.X + size.X, position.Y + size.Y),
UDim2.fromOffset(position.X, position.Y + size.Y)
}
end
local function scale(corners: {UDim2}, parentSize: Vector2): {UDim2}
for i, v in pairs(corners) do
local corner = corners[i]
corners[i] = UDim2.fromScale(
(corner.X.Offset - parentSize.X) / parentSize.X,
(corner.Y.Offset - parentSize.Y) / parentSize.Y
)
end
return corners
end