How to convert number to integer?

The pixel part of udim2 requires integer instead of numbers. How do I convert a variable that takes form as a number to the nearest integer?

3 Likes

UDim2 (in UI properties like Size and Position) automatically rounds the offset values into integers. But if you still need to round them manually, use math.floor()/math.ceil().

3 Likes

Weird thing is that when I insert the variable, it doesn’t work. So I thought it was because of the number issue. By the way, my variable is AbsoluteSize.X I tried printing it in the logs and it printed out a number, just didn’t work when I inserted it into Udim2

1 Like

You can round to the nearest number by using

math.floor(num + 0.5)

6 Likes

I’m not sure I understand how you’re using the data, I feel like you may be writing it incorrectly. How are you setting up the UDim2 variable?

Works when I substitute it with numbers, but doesn’t work with the variable

Are you getting any errors in output?

Can you print the newly create Udim2? (sorry wrong tag)

No. And this is the hierarchy image

Have you confirmed that AbsX*1.78 > AbsY when you’re testing it? I’m not sure what could be wrong at this point (at least nothing caused by the code), it could just be something to do with the UI itself.

Yes I tested it it and it works if I replace AbsX with a number

1 Like

What value is AbsX and what does bg1.Size get set to?

1 Like

AbsX was 1050. and bg1.Size was originally (1,0,1,0)

Then AbsX*1.78 > AbsY must be false, otherwise the OffsetY should be 500 and the ScaleX/ScaleY 0.

1 Like

As I already said. I substituted AbsX with actual numbers and it worked. So the line of code did indeed run.

If you do .Size = UDim2.new(1, 2, 3, 4) and .Size doesn’t end up even resembling that in the slightest, I doubt it actually ran.

1 Like

I did that already and it ran. I knew the line ran, because I set a breakpoint there. When it originally didn’t work, I checked the playergui and the size was changed to (0,0,0,0)

Are you doing it all from the serverscript? Including placing the ScreenGui into the PlayerGui? If so, try using a localscript, and putting it in ReplicatedFirst or just StarterGui.

Since it runs with actual integers inserted, that means it has something to do with the variable itself. I highly doubt using a local script will make a difference considering the fact that it doesn’t require local script to get the AbsoluteSize of a gui

It most likely has something to do with the variable itself, but GUIs should be handled by the client either way, so it’s worth a try.