Roblox recognizing "- 10" as negative number

Hi everyone

I’ve come across an issue where instead of subtracting ten from a GUI’s size, it sets the size to “-10”. How can I fix this?

Some resources:

My code:

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(gui) -- local script already knows local player
	local frame = gui:WaitForChild("Frame")
	
	gui:WaitForChild("Frame").Visible = true
	task.wait(.5)
	frame.Size = UDim2.fromScale(frame.Size.X.Scale, frame.Size.Y.Scale - 10)
end)

The issue: (see the line, which is meant to be the GUI.)
image

The properties: (see the size being -10 on the Y.)
image

If anyone could help, you would be a livesaver. Thanks.

Hey, what is the frame size before this code runs?

You probably meant to use UDim2.fromOffset and use the offset sizes rather than the scale sizes

The scale of this before the code is {200, 0},{50,0}

Edit: And here is a video of the code malfunctioning, even with the fromOffset implemented.

robloxapp-20230420-2129284.wmv (2.0 MB)

If you only need this code once it might be worth it to just hardcore the Gui’s size.

Also, on a side note:

gui:WaitForChild("Frame").Visible = true

Since you already created a variable that waits for Frame, I believe you can just use it in the following lines instead of waiting for child again:

frame.Visible = true

Edit:
In case you want to offset the existing UDim2, perhaps the following may help:

After reading the docs for a bit, it looks like UDim2.fromScale() " Returns a new UDim2 with the given scale components and no offsets."

So, I’m not sure since I’m reading the docs and writing this from my phone so I can’t test it, but this might be the function you’re looking for:
UDim2.fromOffset() “Returns a new UDim2 with the given offset components and no scales.”

try putting this in () brackets maybe will solve it

Edit:
try putting frame size x to its original size in numbers instead of variable

For some reason the axis were inverted before - this is my new code:

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(gui) -- local script already knows local player
	local frame = gui:WaitForChild("Frame")
	
	frame.Visible = true
	task.wait(.5)
	frame.Size = UDim2.fromOffset((frame.Size.X.Scale - 10),(50))
end)

But sadly this does not fix it- it keeps “-10” as the new size.

image

And an updated video showing whats happening now.
robloxapp-20230420-2156317.wmv (1.3 MB)

I won’t be able to hardcore this- it needs to be used many times and at different intervals at random times. I know how to do that stuff just I don’t know why ROBLOX is putting the size as -10 on the X instead of negating 10 from the original number. (which is 200)

I’m not sure if you read the post after i edited, sorry about that. In any case i think this might be what you’re looking for:

Let me know if it doesn’t work! I’ll try to help further when I’m home if you still need help by then

Can you send me a place file? I think there is something about how you’ve set this up that is causing issues

kobob testing.rbxl (48.9 KB)

Heres the place im using.

The reason is because it is executing that code more than once. You are mixing up scale sizes and offset sizes you need to pick one or the other. What your code is doing is taking the 200(a scale value), subtracting 10 for 190 but using that value as an offset value which you now just erased your scale values. If it runs a second time, which it clearly is, your scale value is 0 which when you subtract 10 becomes -10.

You either need to set appropriate scale values or set offset values to start with. When you have 200, 0, 50, 0 you are telling the frame to be 200 TIMES the X size of its parent and 50 TIMES the size of its parents Y size which doesn’t seem reasonable. If you really want to subtract 10 from the sizing you need to change the frame size to (0, 200, 0, 50) by default.

Just remember to put some debounce(or math.min on your new size) in your code or it will shrink quickly being called more than once.

1 Like

It is because of the difference between Scale and Offset:

  • Scale is from 0 to 1, with 0 representing 0% and 1 representing 100%, so making a frame take up the whole screen is easy, you can do it by setting the X and Y scale values to 1. You do this through:
frame.Size = UDim2.fromScale(1, 1)
  • Offset is the number of pixels, between 0 and the width or height of the screen. So, in order to make a frame 100 pixels wide and 50 pixels tall, you do this:
frame.Size = UDim2.fromOffset(100, 50)

The issue is how you are accessing these values. In the image you have provided, both scale values are 0:

image

For example:
{1, 50}, {2, 35}

  • Scale X is 1
  • Scale Y is 2
  • Offset X is 50
  • Offset Y is 35

So, the problem is is that the X Scale is set to 0, and 0 - 10 is -10. The way to fix it is by changing frame.Size.X.Scale - 10 to frame.Size.X.Offset - 10, but there are other problems with the code that I think are worth mentioning:

  • Personally, I would not recommend using Offset, because it is pixel-based and will be different sizes on, say, a 4K screen vs a 1080p screen.
  • You don’t need the extra braces, so:

Should be:

frame.Size.X.Offset - 10, 50

Don’t handle UI on the server! This will only cause unnecessary visual delay for the client and increase the server’s network traffic. Just handle this code on the client through a localscript.

2 Likes