Error with math.abs() - "Number expected, got userdata"

I’ve set up a variable:

self.velocity = vmath.vector3()

I’m attempting to use the absolute value of its x-value:

math.abs(self.velocity.x)

However, this returns the error "Bad argument #1 to ‘abs’ (number expected, got userdata). This happens even when I initialize each of the values to any number:

self.velocity = vmath.vector3(1, 1, 1)

I would appreciate some help on what is causing this!

Strange. Here this works and prints 2:

self.velocity = vmath.vector3(2, 3, 4)
print(math.abs(self.velocity.x))

Can you share a full script with the code that you are running. Perhaps something is altering self.velocity somehow?

1 Like

The full script can be found here.

I think error here

    if math.abs(self.acceleration) > self.accelerationLimit then

because

    self.acceleration = vmath.vector3()

I think I see. In other words, I’m trying to compare a vector to a digit, when they aren’t compatible like that?

You are trying to pass vector3 to math.abs. Error Bad argument #1 to ‘abs’ (number expected, got userdata) is about it. vector3 - is userdata.

1 Like

You probably want to use vmath.length():

if vmath.length(self.acceleration) > self.accelerationLimit then

Or vmath.length_sqr() to avoid a square root operation and get a bit more performance:

if vmath.length_sqr(self.acceleration) > self.accelerationLimit*self.accelerationLimit then
1 Like