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:
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)
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.
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:
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: