GUI corner position

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
Screenshot 2023-04-16 at 15.21.11
How can I get these positions?

1 Like

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.

Screenshot 2023-04-16 at 15.27.01
I position all my guis and size them with scale, so it doesn’t work. also I use Udim2.fromscale just to remove extra code

1 Like

This should do:

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

You can switch it to scale easily, ask me if you need to do so.

1 Like
local corner1 = Frame.Position
local corner2 = Frame.Position + UDim2.new(0, 0, Frame.Size.Y.Scale, 0)
local corner3 = Frame.Position + UDim2.new(Frame.Size.X.Scale, 0, 0, 0)
local corner4 = Frame.Position + UDim2.new(Frame.Size.X.Scale, 0, Frame.Size.Y.Scale, 0)


Pretty interesting result, I think it needs to be in scale. Also shouldn’t it be size/2?

Sure, I’ll add the ability to scale it.
No, it shouldn’t be divided by 2, that will return the center of the guiObject and not the corners.

if its important my anchorpoint is .5,.5

Screenshot 2023-04-16 at 15.34.10

I’m just getting the 4 corners as your asking, can you explain more about what your trying to do? Maybe there’s a better approach.

I’m just trying to position 4 new frames in the corners of another frame.
Here is a snippet from the script creating the frames

for i=1, #Corners do

		local Frame: GuiObject = Instance.new("Frame")
		Frame.Name = "Corner"
		Frame.AnchorPoint = Vector2.new(0.5,0.5)
		Frame.BackgroundColor3 = Color
		Frame.Size = UDim2.fromScale(Thickness, Thickness)
		Frame.Position = Corners[i]
		Frame.Parent = ParentObject

		local constrant = Instance.new("UIAspectRatioConstraint")
		constrant.AspectRatio = Length
		constrant.Parent = Frame

	end

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.


This is the current code

local Corners = scale(getCorners(ParentObject), ParentObject.AbsoluteSize) --Borders.GetCorners(ParentObject, Thickness)

	for i=1, #Corners do

		local Frame: GuiObject = Instance.new("Frame")
		Frame.Name = "Corner+"
		Frame.AnchorPoint = Vector2.new(0.5,0.5)
		Frame.BackgroundColor3 = Color
		Frame.Size = UDim2.fromScale(Thickness, Thickness)
		Frame.Position = Corners[i]
		Frame.Parent = ParentObject

		local constrant = Instance.new("UIAspectRatioConstraint")
		constrant.AspectRatio = Length
		constrant.Parent = Frame

	end

the Scale function doesn’t make a difference :slightly_frowning_face:

I’ll try to reproduce this and try to figure out what’s wrong.

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
2 Likes

@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

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.