Tweening size of a gui object does not tween it to the correct size

Hi developers. I’m having an issue with tweening a gui object.

What I’m attempting to do is tween the size of a gui button to (0.15, 0, 0.15, 0) when clicked. The script then also detects to see if the size of the gui button is equal to the size it was supposed to tween to. However, when I tween the button, it changes the size to (0.150000006, 0, 0.150000006, 0) when clicked which the script is not trying to detect.

My question is, why is it that when I tween the size of a gui object, the new size is very slightly off from what I intended?

I’ve tried looking on the internet and the dev forum for this problem but couldn’t find an answer. The only other thing I’ve tried is changing the line that tweens the size to tweening to (0.150, 0, 0.150, 0) and that hasn’t worked either. Below is the snippet of the code where all the action happens. It’s important to note that I am also changing the rotation of the object at the same time, though I don’t believe that should have any effect. Thank you!

part:TweenSize(UDim2.new(0.15, 0, 0.15, 0), "Out", "Linear", 1.5)
for i = 1, 50 do
	part.Rotation = part.Rotation + 3.6
	wait(0.03)
end
local allnails = true
for a, b in pairs(part.Parent:GetChildren()) do
	if b.Size ~= UDim2.new(0.15, 0, 0.15, 0) then
		allnails = false
	end
end

Note that the script runs without errors and all variables are defined in the full script.

2 Likes

is the bug replicated when using a different resolution / device?
and to my current knowlege, rotation is still rather innactuate / wonky.

I’d set up a clean tween without the rotation to see if that’s the problem, because currently your script doesnt really have any glaring issues.

1 Like

Removing the rotation part did not fix the problem. Testing in studio with no screen emulation, testing in studio with 1920 x 1080 emulation, and testing on my laptop in the actual game all did not work. I don’t have any other devices to test it on at the moment. And it’s all in scale not offset, so I think the resolution wouldn’t pose a problem. Is it possible this could be a studio glitch that needs reporting?

0.000000006 of 4096 (4K screen width) is 0.000024576. That’s an incredibly tiny fraction of a single pixel. Does it really matter?

2 Likes

So it seems like this can largely be chalked up to roblox scaling rather than your own tweening issues. I think the 0.000000006 undesired offset is negligible, but if you want to ensure you’re using exact numbers, use offset.

1 Like

While it may not in some cases and I am able to work around it, I still don’t understand why it would occur. I can modify my code to detect anything above 0.15 or detect anything between 0.15 and 0.1599 but it just doesn’t make sense why it would deviate in the first place.

Yes I suppose the logical answer is that it is a Roblox scaling issue. I’m able to work around it, but in the future it can be critical in some cases where you expect the code to do what you instruct.

I dont see a single case where it matters. Why do you need to programmatically know the offset value? If you want to “detect” whether the button tweened, you can use the callback argument of TweenSize.

You should always expect roblox properties (and Lua numbers in general) to not be exactly precise. Things like floating point errors will occur often.

1 Like

My gui has about 8 buttons. The script is supposed to detect when all 8 buttons are tweened/at the bigger size of 0.15, 0, 0.15, 0. I can’t use the callback on tweensize because the script is checking to make sure every single button has been pressed, and sized. I understand your point however where the amount of cases where this applies may be minimal and that these kinds of inaccuracies occur.

Why can’t you use callbacks?

local tweenedButtons = 0
part:TweenSize(UDim2.new(0.15, 0, 0.15, 0), "Out", "Linear", 1.5, false, function() tweenedButtons = tweenedButtons + 1 end)
--...
if tweenedButtons == 8 then --all
1 Like

Storing the number of callbacks in a variable wouldn’t work in my case. However, it would be able to work if I just store it in an IntValue instead. So thank you for this!

Is this not expected? The way decimals are stored in binary means that precision to the desired amount can’t always be achieved.

(similar to that of setting a NumberValue to 0.2)
image

1 Like

Hey there!

This may not address your issue directly but I see that you are using TweenSize. You should get familiar with TweenService.

With TweenService, you can tween any sort of value from Position and Size to Rotation and Color. It’s a really simple service to use and I suggest you use it over the TweenSize and TweenPosition methods.

EDIT: I realized that I left my hyperlink empty. It should send you to the TweenService reference now.

2 Likes

Thank you for that recommendation! I’ve never experimented with TweenService before so I’m glad I can introduce myself to it now.

1 Like

I have had the same issue with setting NumberValues. I found out that you can use the command bar at the bottom of studio to set the value properly. Just use the following:

PATH_TO_NUMBERVALUE.Value = 0.2
1 Like

You are using Strings instead of Enums

I realize that that’s not great practice and that I should be using Enums but that’s not relevant to the problem or causing any part of it. But thank you for the reminder.

Strings and Enums don’t really have much of a difference. I just find Enums easier to use though.

Nothing you can do about that, just a floating point.

To fix your problem just make a variable set to true whenever you tween the object.