How to get position of ui object relative to screen (absolute position)

I’m making a crafting system like Minecraft and I ran into a problem where the item to be placed does not position correctly. The slots for the crafting are parented in a frame which is smaller than the screen. Since the slots’ positions are relative to the frame, I tried using AbsolutePosition, which didn’t work. Anyone know any solution?

Code that changes the item’s position

for i, slot in pairs(slots:GetChildren()) do
				if MouseHoverOn(slot) then
					
					local desiredAbsolutePosition = slot.AbsolutePosition
					local relativePosition = desiredAbsolutePosition - slot.Parent.Parent.AbsolutePosition
					local Center = UDim2.fromOffset(relativePosition.X, relativePosition.Y)
					
					local CenPos = GetCorner(slot.AbsoluteSize, slot.Position, slot.AnchorPoint)
					item.Position = UDim2.new(Center) --UDim2.new((slot.Position.X.Scale*2), 0, (slot.Position.Y.Scale*2), 0) --UDim2.new(slot.AbsolutePosition.X, 0, slot.AbsolutePosition.Y, 0)
				end
			end

workspace
image

frame size and position
image

I think you have these two mixed up. It would help us if you showed us what it is supposed to look like, and then of course the reality of it.

I’m not certain on this, as I can’t view the interface, but try changing the above line to:

local relativePosition = slot.Parent.Parent.AbsolutePosition - desiredAbsolutePosition

For some reason, this happens


The button is apparently in the exact same place but also in the top left of the screen. I don’t know why this happens

You can use Absoulte Position and get the ViewportSize of client screen to get the absolute scale/position of object on the screen

like that:

local normal = "YOUR PATH TO OBJECT THAT YOU WANT TO GET ABSOLUTE POS/SIZE".AbsolutePosition / workspace.CurrentCamera.ViewportSize
"YOUR OBJECT THAT YOU WANT TO CHANGE".Position = UDim2.new(normal.X, 0, normal.Y, 0)

that might be because of GuiInset, try creating a dummy screengui with a single frame inside with size of 1 by 1 pixel, and position it as 0,0,0,0, now the absolute position of it will result somewhere around (0, 0, 0, -56), u can offset ur target position by the Y inset (-56)

anyway, absoluteposition doesnt care about inset offset, thats why its buggy and u need to use this method