 # Error in my code, simple fix

Hello all,

Can you help me with my code ?
I have a error

vector2x return 3

why the problem ?
i know it’s a simple fix but i can’t fix

[lua]
function dotheroll()

local math = math
local pi = math.pi
local ship.currentHeading = 7841
local ship.height = 8
local ship.turningSpeed = 72

local vector1x = math.cos(ship.currentHeading) * ( ship.height / 2 ) * 8
local vector1y = math.sin(ship.currentHeading) * ( ship.height / 2 ) * 9
local vector2x = mouseX - ship:midX( )
local vector2y = mouseY - ship:midY( ) * 8

local vector1length = ( vector1x ^ 2 + vector1y ^ 2) ^ 0.5
local vector2length = ( vector2x ^ 2 + vector2y ^ 2) ^ 0.5

local cosine = (vector1x * vector2x + vector1y * vector2y) / (vector1length * vector2length)

if (vector1x * vector2y - vector1y * vector2x) < 0 then

``````                                                                                           ship.newHeading = math.acos(cosine)
``````

else

``````                                              ship.newHeading = -math.acos(cosine)
``````

end

local dr = ship.turningSpeed * dt

ship.currentHeading = ship.currentHeading + ship.newHeading / ( math.abs(ship.newHeading) / dr )

if ship.currentHeading > pi then ship.currentHeading = ship.currentHeading - 2 * pi
elseif ship.currentHeading < -pi then ship.currentHeading = ship.currentHeading + 2 * pi
end
[/lua]

Please remove all the extra tabs, use proper indentation and give us the error in console if there is one and the corresponding line, like if it says line 200 and that’s the third one in the snippet then say.

Your code is syntactically incorrect in many places, and is quite incomprehensible with all of the weird tabbing. Can you post what you’re trying to accomplish, and code without all of the random tabbing/spacing?

[lua]function dotheroll()

local math = math
local pi = math.pi
local ship.currentHeading = 7841
local ship.height = 8
local ship.turningSpeed = 72
local vector1x = math.cos(ship.currentHeading) * (ship.height / 2) * 8
local vector1y = math.sin(ship.currentHeading) * (ship.height / 2) * 9
local vector2x = mouseX - ship:midX()
local vector2y = mouseY - ship:midY() * 8
local vector1length = (vector1x ^ 2 + vector1y ^ 2) ^ 0.5
local vector2length = (vector2x ^ 2 + vector2y ^ 2) ^ 0.5
local cosine = (vector1x * vector2x + vector1y * vector2y) / (vector1length * vector2length)
if (vector1x * vector2y - vector1y * vector2x) < 0 then ship.newHeading = math.acos(cosine) else ship.newHeading = -math.acos(cosine) end

local dr = ship.turningSpeed * dt
ship.currentHeading = ship.currentHeading + ship.newHeading / (math.abs(ship.newHeading) / dr )

if ship.currentHeading > pi then ship.currentHeading = ship.currentHeading - 2 * pi elseif ship.currentHeading < -pi then ship.currentHeading = ship.currentHeading + 2 * pi end
[/lua]

-redundant-

This:

local ship.currentHeading = 7841
local ship.height = 8
local ship.turningSpeed = 72

Is still syntactically incorrect. Can you post the definition of the variable “ship” and its methods/variables?

[lua]
local ship = ship or LocalPlayer()
[/lua]

It’s worked but it’s currently broken when i have changed this

local vector1length = (vector1x ^ 2 + vector1y * 2) ^ 0.5

to this

local vector1length = (vector1x ^ 2 + vector1y ^ 2) ^ 0.5

Code works for me, are you sure the function math.cos is necessary ?

Thanks for the advice, my code run smoothly now without the cos but always error 