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.
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?
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.
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.
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.
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!
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.
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:
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.