Multiple problems with converting offset to scale

Edit
To clarifiy, I’m converting offset to scale because the itemframe won’t work properly on different sized screens, such as in this example.
Of course, any other solution other than converting from offset to scale is also welcome.

As the image buttons for the items are created through a script when a player picks up an item, I have to find a way to properly convert the image buttons’ position and size from offset to scale.

function InventoryUI:OffsetToScale(Offset, absoluteSize)
	return UDim2.new(Offset.X/absoluteSize.X, 0, Offset.Y/absoluteSize.Y, 0)
end

function InventoryUI:PositionToSpace(ItemFrame, FirstGrid)
    -- FirstGrid is the top left starting grid
	local pos = FirstGrid.AbsolutePosition - ItemFrame.Parent.AbsolutePosition
	return self:OffsetToScale(pos, ItemFrame.Parent.AbsolutePosition)
	--return UDim2.fromOffset(pos.X, pos.Y)
end

function InventoryUI:CreateItemFrame(FirstGrid)
	local ItemFrame = Instance.new("ImageButton", Backpack.ScrollingFrame)
	ItemFrame.BorderSizePixel = 0
	ItemFrame.Size = UDim2.new(0, 1 * GridSize.X, 0, 1 * GridSize.Y)  -- Otherwise, ItemFrame.AbsoluteSize is 0,0
	ItemFrame.Size = self:OffsetToScale(ItemFrame.AbsoluteSize, ItemFrame.Parent.AbsoluteSize)
end)

However, clearly, something isn’t working as the resulting ItemFrames scaled position and size is this.
It should be fitting perfectly along the first column of the inventory grid.

This is the current hierarchy in PlayerGui, where the image button is the ItemFrame, and the Grid frame contains all the grids in the Inventory.
image

1 Like

I can’t see the screenshot you uploaded to Gyazo. It’s just blank on my end. Is there any way you could copy and paste it here?

https://gyazo.com/61fab2c3e0bcff1b1c0729a024c59274

I’m afraid I still can’t see anything.

What I mean is copy your image by right-clicking it and clicking “Copy” and then just pasting it in a reply here. It should automatically generate within it.

It’s not a picture, it’s a video/GIF. I can’t copy and paste it as an image.
This sounds like a problem on your end.

I’m not sure if this would work in your scenario, but try using UIListLayouts . Then, you could probably clone the frames when the player collects a new item.

Are you trying to set up items in singular boxes?

When the player picks up an item, it will create an item image that changes in size depending on how many grids it should occupy. E.g:
image

When creating the image buttons, I want them to scale depending on the user’s screen size, hence why I want to convert offset to scale.

1 Like