Math error on PhysicsSimulate

Hi, i’m changing my flight code on my Entity and i’m getting this error:



includes/extensions/math.lua:203: attempt to compare nil with number


Here’s my code:
[lua]
local ZAxis = Vector(0,0,1);
function ENT:PhysicsSimulate( phys, deltatime ) --######### @ Catdaemon,RononDex,aVoN

self.Accel={}
local FWD = self:GetForward()
local UP = ZAxis
local RIGHT = FWD:Cross(UP):Normalize()

if self.Pilot and self.Pilot:IsValid() then
	if self.Inflight then
		
		if self.epodo and self.Pilot:KeyDown(IN_FORWARD) and self.Engine == true then
			num=1700
		elseif not self.epodo and self.Engine and self.Pilot:KeyDown(IN_FORWARD) then
			num=700
		elseif not self.Engine and self.Pilot:KeyDown(IN_FORWARD) then
			num=700
		elseif self.Pilot:KeyDown(IN_BACK) then
			num=-500
		else
			num=0
		end
		self.Accel.FWD=math.Approach(self.Accel.FWD,num,5.5)
		
		if self.Pilot:KeyDown(IN_MOVERIGHT) then
			num2 = 650
		elseif self.Pilot:KeyDown(IN_MOVELEFT) then
			num2 = -650
		else
			num2=0
		end
		self.Accel.RIGHT=math.Approach(self.Accel.RIGHT,num2,5.5)
		
		if self.Pilot:KeyDown( IN_JUMP ) then
			num3 = 650
        elseif self.Pilot:KeyDown( IN_DUCK ) then
			num3 = -650
		else
			num3=0
        end
		self.Accel.UP=math.Approach(self.Accel.UP,num3,5.5)
		
		phys:Wake()
		if not self.Hover then
		if self.Accel>-200 and self.Accel < 200 then return end --with out this you float
		end

		local pr={
		secondstoarrive	= 1;
		pos = self:GetPos()+(FWD*self.Accel.FWD)+(UP*self.Accel.UP)+(RIGHT*self.Accel.RIGHT);
		maxangular		= 9000;
		maxangulardamp	= 1000;
		maxspeed			= 1000000;
		maxspeeddamp		= 500000;
		dampfactor		= 1;
		teleportdistance	= 5000;
		}
		local ang = self.Pilot:GetAimVector():Angle()
		local pos = self:GetPos()
		
		local velocity = self:GetVelocity()		
		local aim = self.Pilot:GetAimVector();
		local ang = aim:Angle();
		local ExtraRoll = math.Clamp(math.deg(math.asin(self:WorldToLocal(pos + aim).y)),-25,25); -- Extra-roll - When you move into curves, make the shuttle do little curves too according to aerodynamic effects
		local mul = math.Clamp((velocity:Length()/1700),0,1); -- More roll, if faster.
		ang.Roll = (ang.Roll + self.Roll - ExtraRoll*mul) % 360;
		pr.angle			= ang --+ Vector(90 0, 0)
		pr.deltatime		= deltatime
					
		phys:ComputeShadowControl(pr)
	end
end

end
[/lua]

Thanks

I see 2 things wrong here. First you’re defining self.Accel inside PhysicsSimulate. This will reset it every time it calculates, and I don’t think that’s what you want to do. You should put it in Initialize instead.
Second, you haven’t defined self.Accel.FWD/RIGHT/UP anywhere, so it complains when math.Approach tries to compare with it since it’s nil.

So basically, remove line 4 and add this to ENT:Initialize()
[lua]
self.Accel={FWD=0, RIGHT=0, UP=0}
[/lua]
:keke:

Thanks this worked like a charm. Much appreciated.