What's wrong with this line of code?

So this line of code errors for some reason.

Scrubber.Position = Scrubber.Position * UDim2.new(1, 0, 1, 1) + UDim2.new(0, Mouse.X, 0, 0)

This is the error this line gives:

19:56:09.908 - Players.C0lvy123.PlayerGui.Slider.LocalScript:28: attempt to perform arithmetic (mul) on userdata

Scrubber is an image button, and Mouse is the player’s mouse I got using GetMouse (this is a local script). Why am I getting this error?

I’m guessing it isn’t possible to multiply this?
Scrubber.Position * UDim2.new(1, 0, 1, 1) + UDim2.new(0, Mouse.X, 0, 0)
And add? (I might be wrong though)

Basically, this means it can’t do the math:

attempt to perform arithmetic (mul) on userdata

You can probably fix this by separating the values?

1 Like

But what’s wrong with multiplying that? I know you’re able to multiply vector3s, why would udim2s be different?

Vector3s have 3 values I guess whereas UDIm2’s are special in the case they have both {} and , and different values? Idk, never had a case to do this.

Actually, its probably because you’re multiplying POSition (VECTOR3) and UDim2 (DIFFERENT)

1 Like

You can’t add Udim2 values or mutiply them. Same goes to Vectors.
Instead do the following.

Scrubber.Position = Scrubber.Position * UDim2.new(1, 0 + Mouse.X, 1, 1) 

Add in the Udim2 section in the parenthesis.

You can add vectors and multiply them, though. You can also subtract and divide them.

However, you cannot do those arithmetics (excluding multiply) on CFrames.

3 Likes

You’re essentially doing this:

local Position = Vector3.new(1, 1, 1)
local Udim2 = UDim2.new(1, 0, 1, 0)

print(Position * Udim2)

Which won’t work. Think of having 4 and 3 values. If you multipled 3 values with randomly another 4 values- oh wait you can’t because thats not really possible. You’ll have to assign which values you want to multiply with which instead.
image

Also:

local Position = Vector3.new(1, 1, 1)

print(Position * Position)

Works because they’re both the same value type.

1 Like

I used ot get errors while multiplying and substracting Vectors. Thanks for the heads up.
@Fifkee

1 Like

Actually Ui.Position returns a Udim2, not a vector3. So in my case it would be multiplying a udim2 and a udim2, not a vector3 and a udim2.

So I think I’ve narrowed down the problem, I tested it out somewhere else and it errors when I multiply udim2s but when I add udim2s it works.

So I found a workaround and I did what I wanted it to do without multiplying udim2s and it doesn’t error, thanks to everyone who tried to help me out.

The UDim2 datatype only has arithmetic operators for adding and subtracting, multiplication isn’t built into them:


https://developer.roblox.com/en-us/api-reference/datatype/UDim2

Datatypes have special functionalities that have to be built into them. Vector3’s multiplication is a built in method. A computer can’t just assume what you want it to do, it has to be told exactly what to do.

4 Likes

Thanks, I’ll mark that as the solution because it fully explains what was wrong.