Is it possible to devide the size of a UI element?

Hello, I have been working on my game and I was trying to make all of my UI animated for example when the player presses a button the button shrinks and then goes back to it’s normal size and I am tired of scaling down my ui and copying the size and puting it on a script. So is it possible to instead of doing this:



local tweenservice = game:GetService('TweenService')

local tweenInfo = TweenInfo.new(
    .2,
    Enum.EasingStyle.Sine,
    Enum.EasingDirection.InOut,
    0,
    true,
    0
)

local tween = tweenservice:Create(script.Parent, tweenInfo, {Rotation = 3})

script.Parent.MouseEnter:Connect(function()
    tween:Play()
end)

script.Parent.MouseButton1Click:Connect(function()
    local sizeTween= tweenservice:Create(script.Parent, tweenInfo, {Size = UDim2.new(0.586, 0,0.159, 0)}):Play()
end)

Just deviding the size by a number?

Thank you!

1 Like

I’m pretty sure to divide you can just do 1/2

1 Like

You can divide each component of an UDim2 by using .X and .Y and then getting the Scale and Offset for each and divide those by 2 to halve them

1 Like

This returns an error:

local currentSize = script.Parent.Size

local sizeX = currentSize.X/2

local sizeY = currentSize.Y/2

local tweenservice = game:GetService('TweenService')

local tweenInfo = TweenInfo.new(
    .2,
    Enum.EasingStyle.Sine,
    Enum.EasingDirection.InOut,
    0,
    true,
    0
)

local tween = tweenservice:Create(script.Parent, tweenInfo, {Rotation = 3})

script.Parent.MouseEnter:Connect(function()
    tween:Play()
end)

script.Parent.MouseButton1Click:Connect(function()
    local sizeTween = tweenservice:Create(script.Parent, tweenInfo, {Size = UDim2.new(sizeX, sizeY)}):Play()
end)

You have to do it for all 4 of those

local SizeXScale = currentSize.X.Scale/2
local SizeXOffset = currentSixe.X.Offset/2
local SizeYScale = currentSize.Y.Scale/2
local SizeYOffset = currentSize.Y.Offset/2

Or in your case, just the Scales for x and y since you’re not usign the offsets

1 Like

I am not using offset so I just do this?:

local currentSize = script.Parent.Size

local sizeX = currentSize.X/2

local sizeY = currentSize.Y/2

local tweenservice = game:GetService('TweenService')

local tweenInfo = TweenInfo.new(
    .2,
    Enum.EasingStyle.Sine,
    Enum.EasingDirection.InOut,
    0,
    true,
    0
)

local tween = tweenservice:Create(script.Parent, tweenInfo, {Rotation = 3})

script.Parent.MouseEnter:Connect(function()
    tween:Play()
end)

script.Parent.MouseButton1Click:Connect(function()
    local sizeTween = tweenservice:Create(script.Parent, tweenInfo, {Size = UDim2.new(sizeX,0, sizeY,0)}):Play()
end)

It returns an error.

local sizeX = currentSize.X.Scale/2

local sizeY = currentSize.Y.Scale/2

You forgot to do .Scale

The X and Y components of a UDim2 have a Scale and Offset in them, you need to specify which one you want to use

2 Likes

Thank you! It works perfectly!

1 Like

Anytime! If you have anymore issues don’t be afraid to make another post!

1 Like