How to change only the Y value of a UDim2 with a number (fixed)

So i’m trying to make a trunk, sort of like a backpack where you can store items. The only problem is that when I try to change the Y value of a textbutton with a number it says this error:
image

Here’s the script:

value = 0
script.Parent.MouseButton1Click:Connect(function()
trunk = script.Parent.Parent.trunk:Clone()
trunk.Position = UDim2.new({0.211, 0},{value, 0})
value = trunk.Position.Y + 77

end)

Thanks for any help!

3 Likes

For UDim2.new() you do:

UDim2.new(0.211,0,value,0)

not

UDim2.new({0.211, 0},{value, 0})

Hope this helped.

3 Likes

Thanks! Sadly it still shows the same error.

For clarification, would you mind pointing out which line is line 8?

Maybe try setting value to a UDim2 value and then position = position*value.

It doesn’t really matter to the problem i’m having, but it is used to make it so when I click the button and it clones it again, it doesn’t overlap with the previously cloned button.

Here’s a photo of the gui if that helps:

image

Is this what you are talking about?

image

The UDim2 constructor function takes 4 parameters: the X scale, the X offset, they Y scale, and the Y offset.
For more info, check out the API Page for the UDim2 object.

There’s an issue with the statement where you declare the position of trunk, as there is no need for UDim2.new. The UDim2.new function returns a UDim2 value, and value2 is already a UDim2 value.
Replace it to this:

trunk.Position = value2

None of the replies I’ve seen have been addressing the actual issue:

You are trying to run a mathematical equation without the proper components.

value = trunk.Position.Y+77

You are trying to add a UDim with a number, hence the error of “UDim expected, got number”.

Let’s think of a UDim as a table, it has two components: Scale and Offset. You can’t add one number to it, but you can add to one of it’s components individually.

To fix this:

value = trunk.Position.Y.Scale + 0.077

I’m assuming 0.077 is what you actually want since you aren’t working with pixels and are working with a percentage of the screen so I have changed it preemptively, you can easily adjust it directly with that code without changing anything else.

If you do want to work with pixels though, here is the code to do so:

value = trunk.Position.Y.Offset + 77
4 Likes

It works! Thank you very much!

1 Like