Absolute position not working

im trying to set the absolute position of the blue square to the red square but it doesn’t work

for screen gui, i clicked on ignore gui inset
and also for the 2 squares they both have an anchor point of (0.5, 0.5)
image
image

local gui = script.Parent
local Red = gui.Red
local Blue = gui.Blue

local AbsPos = Red.AbsolutePosition

Blue.Position = UDim2.new(0, AbsPos.X, 0, AbsPos.Y)

print(Red.AbsolutePosition)
print(Blue.AbsolutePosition)

the output
image
it gives constant results

here’s the file
abspos.rbxm (3.7 KB)

1 Like

my mistake was that i set the anchor point of the blue one to be 0.5, 0.5 and also the ignore gui inset also affects it

ok here’s the solution if you want anchor point to be .5,.5 and also account for ignore gui inset
I added a marker to check if its correct
image
also setting ui position, it always rounds to the nearest whole number, so it doesn’t perfectly aligns

local GuiService = game:GetService("GuiService")

local gui = script.Parent
local marker = gui.Marker

local Red = gui.Red -- Replace "ParentObject" with the name of your parent object
local Blue = Red.Blue -- Replace "ChildObject" with the name of your child object

Blue.AnchorPoint = Vector2.new(0.5, 0.5)

local inset1, inset2 = GuiService:GetGuiInset()
local insetOffset = inset1 - inset2

local topLeftPosition = Blue.AbsolutePosition + insetOffset
local size = Blue.AbsoluteSize
local centerPosition = topLeftPosition + size / 2

print("Center position of child object: " .. tostring(centerPosition))

marker.Position = UDim2.new(0, centerPosition.X, 0, centerPosition.Y)

squaretest.rbxm (4.2 KB)

3 Likes

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