Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would i calcucate half the size of an udim2?

Hi, I'm making a billboard gui. When a player gets close, the billboard gui becomes twice as small. But how can I calculate how big it should be when it is twice as small?

for i, child in pairs(billboardgui:GetChildren()) do
	local halfsize = child.Size <==
	child:TweenSize(halfsize)
end

2 comments

1

UDim2 values consist of, to my knowledge: UDim2.X, UDim2.X.Offset, UDim2.Y, UDim2.Y.Offset. If this is not correct, please inform me.

0

Could you give an example? I only worked with TweenService on Frame tweening positions

3 answers

✓ Accepted answer

As per the API reference and what DeceptiveCaster said, using a UDim2 involves four properties: xScale, xOffset, yScale, & yOffset

ex. a GuiObject with the size of UDim2.new(1, 0, 1, 0) will take up 100% of the space (both x and y axes) of the parent object.

A potential way to get half the size of a UDim2 is to subtract the the value of the scale properties by 0.5. (Using a math operation between the size of the GuiObject and a new UDim2 with 50% scale on whichever axes you want to halve.)

https://developer.roblox.com/en-us/api-reference/datatype/UDim2#math-operations

1 comment

1

Thanks

This might help:

local billboardgui = script.Parent.BillboardGui
wait(5)
--// function for dividing Scale and offset of a UDim2 vector space
function HalfUDim2(sze)
	local UD2W = sze.Width
	local UD2H = sze.Height
	--// make sure any element are Not 0 if they are, then just set Half Scale/Offset to 0 
	--// if we didn't check this we would be dividing by 0 which is impossible!
	--// format    Scale not eq = 0 then  set Scale/2 	or set it to 0
	local XS = UD2W.Scale ~=0 	and UD2W.Scale/2 	or UD2W.Scale
	local XP = UD2W.Offset ~= 0 and UD2W.Offset/2 	or UD2W.Offset
	local YS = UD2H.Scale ~=0 	and UD2H.Scale/2 	or UD2H.Scale
	local YP = UD2H.Offset ~= 0 and UD2H.Offset/2 	or UD2H.Offset
	return UDim2.new(XS,XP,YS,YP)
end
for i, child in pairs(billboardgui:GetChildren()) do
	local halfsize = HalfUDim2(child.Size)
	local halfpos = HalfUDim2(child.Position)
	child:TweenSizeAndPosition(halfsize, halfpos) -- changed this as we're changing the size of each element, what you had before only resized the elements but not change the position so i've included it into this equation with the above function!
end

let me know if you need more info!

Hope this helps!

1 comment

0

Exactly what i needed! :D

for i, child in pairs(billboardgui:GetChildren()) do
	local halfsize = child.Size/2
	child:TweenSize(halfsize)
end

2 comments

0

It gives an error in my output and does not work: ''Attempt to perform arithmetic (div) on UDim2 and number.''

0
  1. Don't spoonfeed. 2. This is not even close to being correct. A UDim2 consists of 4 numbers, and you're assuming there is 1 number prevalent within the UDim2. This will never work, no matter what the values are. (This is also an issue of a UDim2 not being of type number.)

This discussion is preserved as it was. New answers, comments, and votes are closed.