Help With Variables - Seriously Bugging Me

I am making a SWEP. All I want it to do right now is change the viewmodel position when I right click. For some reason, the variable “test” does not change in the if statement in SWEP:SecondaryAttack().
The viewmodel’s position doesn’t change either.
Someone please help, it is starting to fuck with me.

local test = Vector(0,0,0)

function SWEP:SecondaryAttack()

if (self:GetOwner():KeyPressed(IN_ATTACK2) && !aiming) then
	aiming = true
	test = Vector(1,0,0)
elseif (self:GetOwner():KeyPressed(IN_ATTACK2) && aiming)then
	aiming = false
	test= Vector(100,0,0)


 function SWEP:GetViewModelPosition( pos, ang )
	pos = pos + test
	return pos, ang

All of the rest of my SWEP is working correctly, just this is frustrating me.

Are you testing in singleplayer? SecondaryAttack doesn’t get called on the client in singleplayer, so you’ll have to either test in a MP server or network the value.
Also - don’t use upvalues for SWEPs, they’re shared across all instances which is not what you want. Use the self object instead:
self.test = Vector(1,0,0)
pos = pos + self.test

Thank you, I didn’t know SecondaryAttack didn’t get called on the client. This may sound dumb but what is an upvalue?

A local that’s in a function’s closure scope but not in it’s private scope
[lua]myglobal = 1 – any function can see this
local myupvalue = 2 – any function below this line can see this variable
function myfunc()
local mylocalupvalue = 3 – this is a local for myfunc but an upvalue for myfunc2
function myfunc2()
– this function can see mylocalupvalue
function myfunc3()
– this function can’t see mylocalupvalue (but it can see myupvalue)

So, if I wrote this for instance:

aiming = false

function SWEP:SecondaryAttack()

    if (self.Owner:KeyPressed(IN_ATTACK2)) then
        aiming = true


Then when I right click, it should change the global aiming to true?

Note that self is only created for functions defined as part of an object with the colon (:slight_smile: operator.
[lua]local obj = {}
function obj:objFunction()
– self automagically created and set as an invisible first argument
function obj.notObjFunction(self)
– no self, have to create it manually
function loosefunction()
– you’re on your own! No self available here

obj:objFunction() – obj is invisibly passed as the first argument - self.
obj.objFunction() – self is nil!
obj.objFunction(obj) – if you use . instead of : you can pass to the invisible argument like it’s normal. self is now obj
obj:notObjFunction() – the normal first argument is invisibly passed obj[/lua]

[editline]4th March 2013[/editline]

Oh, you fixed your mistake. :v:
Well, still informative info.

So to get the viewmodel to change when aiming is true, I would have to use a networked variable, correct?
I definitely think my problem is that SWEP:GetViewModelPosition() is only called on the client, so I can’t call or set the value of aiming.
Thanks so much for your help by the way.

I GOT IT TO WORK. Thank you.
I forgot I was using if SERVER in the secondary attack hook so it was only setting the serverside aiming boolean, that is why the viewmodel changing (clientside) wasn’t working, because the aiming variable never changed there.

I’m an idiot.
Thank you for your help.