Problem with part of my SWEP code


   function SWEP:GetViewModelPosition( pos, ang )

   pos = pos + ang:Right() * 2 + ang:Up() * -0.30
   return pos
   
   if self.Owner:KeyDown( IN_SPEED ) then
   pos = pos + ang:Right() * 2 + ang:Up() * 0.75
   return pos
   end

   end


I get this error:


[ERROR] lua/weapons/weapon_ak74mor.lua:232: 'end' expected (to close 'function' at line 227) near 'if'
  1. unknown - lua/weapons/weapon_ak74mor.lua:0



  1. What’s wrong?

  2. How can I improve upon this code?

Is that the last function in your file? Might be a function that is not closed properly.

There are two functions after it, then the code ends. Let me try putting it earlier in the code…

[editline]6th May 2015[/editline]

Didn’t work.

Can you post the whole code please?

you can’t have code in a same chunk after a return.

[lua]
function SWEP:GetViewModelPosition( pos, ang )

if self.Owner:KeyDown( IN_SPEED ) then
	pos = pos + ang:Right() * 2 + ang:Up() * 0.75
	return pos
end

pos = pos + ang:Right() * 2 + ang:Up() * -0.30
return pos

end
[/lua]

Explanation: Returning from a function is the last thing that should be done. It pushes the returned values back onto the stack, and jumps back to where the function was called. If there was code after the return inside the same chunk, it would never get called, and therefore it isn’t expected, and shows up as a syntax error. The code I posted has a return inside an if statement, which is in a separate chunk. The same logic applies to “break” and “continue”.