Keeping a UI element "centred"

I’m trying to make a local script adjusting the size of a GUI element upon the entry of the players mouse. However, just like with regular GUI sizing, the size increases to the right and towards the bottom of the screen (as opposed to the size increase being equally distributed to the left and right and bottom and top of the UI element).

Here's a video to help better understand

.

And here's the script

script.Parent.MouseButton1Down:Connect(function()
local marketPlaceService = game:GetService(“MarketplaceService”)
marketPlaceService:PromptProductPurchase(game.Players.LocalPlayer, 1658627474)
end)

local button = script.Parent
local originalSize = button.Size

local originalPosition = button.Position

print (originalPosition)

local scaleFactor =1.1

local function onMouseEnter()
local newOffsetX = originalPosition.X.Offset - (originalSize.X.Offset * (scaleFactor - 1) / 2)
local newOffsetY = originalPosition.Y.Offset - (originalSize.Y.Offset * (scaleFactor - 1) / 2)

button.Size = UDim2.new(originalSize.X.Scale * scaleFactor, originalSize.X.Offset * scaleFactor, originalSize.Y.Scale * scaleFactor, originalSize.Y.Offset * scaleFactor)

button.Position = UDim2.new(originalPosition.X.Scale, newOffsetX, originalPosition.Y.Scale, newOffsetY)

end

local function onMouseLeave()
button.Size = UDim2.new(originalSize.X.Scale, 0, originalSize.Y.Scale, 0)
button.Position = UDim2.new(originalPosition.X.Scale, 0, originalPosition.Y.Scale, 0)
end

button.MouseEnter:Connect(onMouseEnter)
button.MouseLeave:Connect(onMouseLeave)

What can I do to keep the element “centred” instead of “visually moving”?

1 Like

Try changing the “AnchorPoint” property to (0.5, 0.5)

5 Likes

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