Vector and Angle Data: "number expected, got userdata"

I’ve had this problem with both my own code and code a person has given me in another help thread.

Here’s the example I was given:

[lua]function ForwardKey(ply, key)
local vec1 = ply:GetAimVector()
timer.Simple(1,function() local vec2 = ply:GetAimVector()
if math.deg(Angle(vec1,vec2)) > 90 then – they turned 90 degrees in 1 second
ply:SetVelocity( (ply:GetAimVector()*5000)-Vector(0, 0, (ply:GetAimVector()*5000).z))
end
end )
end
hook.Add(“KeyPress”, “Forward Key Pressed”, ForwardKey)[/lua]


[ERROR] .../.../gamemode/init.lua:9: bad argument #1 to 'deg' (number expected, got userdata)

Here’s the version from my code:

[lua]function ForwardKey(ply, key)
local standingon = ply:GetGroundEntity()
if standingon:IsWorld() or standingon:IsValid() then
if key == IN_MOVELEFT then
ply:SetVelocity( (ply:EyeAngles():Right()-800)-Vector(0, 0, (ply:EyeAngles():Right()-800).z))
playerLeftAcel = 1
local playermouseaim1 = ply:GetAimVector()
timer.Create(“leftkeydown”, 0.2, 0, function()
if playerLeftAcel == 1 then
if ply:KeyDown(IN_MOVELEFT) != true then
playerLeftAcel = 0
end
if ply:KeyDown(IN_MOVERIGHT) == true then
if standingon:IsWorld() or standingon:IsValid() then
local playermouseaim2 = ply:GetAimVector()
playerLeftAcel = 0
playerRightAcel = 0
playerForwardAcel = 1
–ply:SetVelocity( (ply:GetAimVector()*800)-Vector(0, 0, (ply:GetAimVector()*800).z))
if math.deg(Angle(playermouseaim1,playermouseaim2)) > 30 then
ply:SetVelocity( (ply:GetAimVector()*5000)-Vector(0, 0, (ply:GetAimVector()*5000).z))
end
end
end
end
end )
end
end
end
hook.Add(“KeyPress”, “Forward Key Pressed”, ForwardKey)[/lua]

same error:


[ERROR] .../.../gamemode/init.lua:47: bad argument #1 to 'deg' (number expected, got userdata)

Might be helpful if you actually read the errors in which you’re getting.
It’s complaining about the arguments being fed to math.deg. If you look at the code it’s being fed an Angle object (which, on its own, only has pitch and yaw filled, and doesn’t have a roll component)

math.deg takes a number (which in this case is Radians), not an Angle.

I’m only going off what was given to me in a previous thread.

What would I need to do to work out the difference between two vectors?

The wiki is a thing. Use it. It has all this stuff documented.

Changed line 47 to [lua]if math.deg(playermouseaim1-playermouseaim2) > 30 then[/lua] still get the same error. Also tried changing line 47 to [lua]if playermouseaim1-playermouseaim2 > ((playermouseaim1+playermouseaim2)/2) then[/lua] but I get this error:


[ERROR] .../.../gamemode/init.lua:47: attempt to compare two userdata values

The only fix I’ve found so far is using :Length() after everything, but apparently that screws up player aim vectors by making them all equal to 1.

Edit:

trying to use [lua]if playermouseaim1:Sub(playermouseaim2) > ((playermouseaim1:Add(playermouseaim2))/2) then[/lua] gives me


[ERROR] .../.../gamemode/init.lua:47: attempt to perform arithmetic on a nil value

Edit2:

Correct me if I’m wrong here, but if you set a variable like “NumText = 12” then put it in a function that was set up like this: “NumMathFunction( NumberGoesHere)”, wouldn’t “NumMathFunction(NumText)” be the same as “NumMathFunction(12)”? I know it seems like a stupid question, but I seem to keep having a problem where if I put anything down that isn’t literally a number/vector/angle, even if it’s a variable that’s set to a number/vector/angle, I get errors for inserting userdata.