SWEP Variables not updating

I’m making a swep using the SWEP construction kit. I’m trying to modify the angles of two gears on the front of my weapon every second.

The actual maths behind it makes sense.

It’s all physics. The angle (Gears_Angle) is itself plus the (velocity (gears_angularvelocity) PLUS the acceleration (gears_angularacceleration)). However, my SWEP does not like this.
The code is as follows:


-- Variables that are used on both client and server

AddCSLuaFile()


SWEP.Author			= "Johnny Mc Boab"
SWEP.Instructions	= "I don't bloody know"


SWEP.Spawnable			= true
SWEP.UseHands			= true


SWEP.ViewModel			= "models/weapons/v_physcannon.mdl"
SWEP.WorldModel			= "models/weapons/w_pistol.mdl"
SWEP.HoldType 			= "physgun"
SWEP.ViewModelFOV 		= 60
SWEP.ViewModelFlip 		= false


SWEP.Primary.ClipSize		= -1
SWEP.Primary.DefaultClip	= -1
SWEP.Primary.Automatic		= true
SWEP.Primary.Ammo			= "none"


SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= "none"


SWEP.AutoSwitchTo		= false
SWEP.AutoSwitchFrom		= false


SWEP.PrintName			= "Guntacle"
SWEP.Slot				= 3
SWEP.SlotPos			= 3
SWEP.DrawAmmo			= false


ShootSound = Sound( "weapons/minigunne.wav" )






SWEP.ViewModelBoneMods = {
	["Prong_B"] = { scale = Vector(0.009, 0.009, 0.009), pos = Vector(0, 0, 0), angle = Angle(0, 0, 0) },
	["Doodad_1"] = { scale = Vector(0.009, 0.009, 0.009), pos = Vector(0, 0, 0), angle = Angle(0, 0, 0) },
	["Prong_A"] = { scale = Vector(0.009, 0.009, 0.009), pos = Vector(0, 0, 0), angle = Angle(0, 0, 0) },
	["Doodad_2"] = { scale = Vector(0.009, 0.009, 0.009), pos = Vector(0, 0, 0), angle = Angle(0, 0, 0) },
	["Doodad_3"] = { scale = Vector(0.009, 0.009, 0.009), pos = Vector(0, 0, 0), angle = Angle(0, 0, 0) },
	["Handle"] = { scale = Vector(1.22, 1.22, 1.22), pos = Vector(0, 0, 0), angle = Angle(0, 0, 0) },
	["Doodad_4"] = { scale = Vector(0.009, 0.009, 0.009), pos = Vector(0, 0, 0), angle = Angle(0, 0, 0) }
}


SWEP.VElements = {
	["x2"] = { type = "Model", model = "models/props_lab/reciever01d.mdl", bone = "Base", rel = "", pos = Vector(0, 0.455, -9.546), angle = Angle(-144.206, 90, 0), size = Vector(0.5, 0.5, 0.5), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} },
	["Motor"] = { type = "Model", model = "models/props_wasteland/light_spotlight01_lamp.mdl", bone = "Base", rel = "", pos = Vector(7.727, 5.908, 14.09), angle = Angle(90, 166.705, 0), size = Vector(0.4, 0.4, 0.4), color = Color(255, 255, 255, 255), surpresslightning = false, material = "models/props_canal/metalwall005b", skin = 0, bodygroup = {} },
	["Wire"] = { type = "Model", model = "models/props_vehicles/carparts_muffler01a.mdl", bone = "Base", rel = "", pos = Vector(1.363, 10.454, -9.546), angle = Angle(-86.932, -52.16, 0), size = Vector(1.003, 1.003, 1.003), color = Color(255, 255, 255, 255), surpresslightning = false, material = "models/props_pipes/pipeset_metal", skin = 0, bodygroup = {} },
	["Base"] = { type = "Model", model = "models/props_junk/plasticbucket001a.mdl", bone = "Base", rel = "", pos = Vector(0.455, 2.273, 5.908), angle = Angle(0, 90, 0), size = Vector(0.833, 0.833, 0.833), color = Color(255, 255, 255, 255), surpresslightning = false, material = "models/props_canal/metalwall005b", skin = 0, bodygroup = {} },
	["GearSmall"] = { type = "Model", model = "models/Mechanics/gears/gear12x12.mdl", bone = "Base", rel = "", pos = Vector(9.545, 5.908, 17.726), angle = Angle(0,0,0), size = Vector(0.3, 0.3, 0.3), color = Color(255, 255, 255, 255), surpresslightning = false, material = "models/props_pipes/pipeset_metal", skin = 0, bodygroup = {} },
	["X"] = { type = "Model", model = "models/props_wasteland/controlroom_filecabinet001a.mdl", bone = "Base", rel = "", pos = Vector(6.817, 4.8, 14.09), angle = Angle(0, -15.341, 0), size = Vector(0.321, 0.321, 0.18), color = Color(255, 255, 255, 255), surpresslightning = false, material = "models/props_canal/metalwall005b", skin = 0, bodygroup = {} },
	["GearBig"] = { type = "Model", model = "models/Mechanics/gears/gear16x12.mdl", bone = "Base", rel = "", pos = Vector(1.363, 1.363, 16.818), angle = Angle(0,0,0), size = Vector(0.3, 0.3, 0.3), color = Color(255, 255, 255, 255), surpresslightning = false, material = "models/props_pipes/pipeset_metal", skin = 0, bodygroup = {} },
	["ScreenQuad"] = { type = "Quad", bone = "Base", rel = "", pos = Vector(0.36, -2.62, -7.801), angle = Angle(0, 0, 30), size = 0.082, draw_func = nil},
	["ScreenModel"] = { type = "Model", model = "models/props_lab/monitor01b.mdl", bone = "Base", rel = "", pos = Vector(0, -1.364, -5.909), angle = Angle(-60, 90, 0), size = Vector(0.349, 0.349, 0.349), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} }
}




/*---------------------------------------------------------
	Reload does nothing
---------------------------------------------------------*/
function SWEP:Reload()
end


/*---------------------------------------------------------
   Think does nothing
---------------------------------------------------------*/
function SWEP:Think()
    self:updateAngles()
    self.VElements["GearSmall"].angle.y = self.Gears_Angle
    self.VElements["GearBig"].angle.y = self.Gears_Angle


end


function SWEP:updateAngles()
    self.Gears_Angle = self.Gears_Angle + math.Clamp(self.Gears_AngularVelocity + self.Gears_AngularAcceleration,0,1000)
end


/*---------------------------------------------------------
	PrimaryAttack
---------------------------------------------------------*/
function SWEP:PrimaryAttack()


	self:SetNextPrimaryFire( CurTime() + 0.2 )
	self:EmitSound(ShootSound,500,math.random(5)+97)
	self:ShootEffects( self )
    self.Gears_AngularVelocity = 1
    self.Gears_AngularAcceleration = -0.1
    MsgN( "Angle:" ..tostring(self.Gears_Angle) )
    MsgN( "Angle_V:" ..tostring(self.Gears_AngularVelocity) )
    MsgN( "Angle_A:" ..tostring(self.Gears_AngularAcceleration) )


end


/*---------------------------------------------------------
	SecondaryAttack
---------------------------------------------------------*/
function SWEP:SecondaryAttack()
	
end




/*---------------------------------------------------------
   Name: ShouldDropOnDie
   Desc: Should this weapon be dropped when its owner dies?
---------------------------------------------------------*/
function SWEP:ShouldDropOnDie()
	return false
end


function SWEP:Initialize()
		self:SetDeploySpeed(0.7)
		self.SpinUpTime = 1
		self.SmallGearRatio = 360/12
		self.BigGearRatio = 360/16
        self.Gears_Angle = 0
        self.Gears_AngularVelocity = 0
        self.Gears_AngularAcceleration = 0


        // other initialize code goes here
         if CLIENT then
        
                // Create a new table for every weapon instance
                self.VElements = table.FullCopy( self.VElements )
                self.WElements = table.FullCopy( self.WElements )
                self.ViewModelBoneMods = table.FullCopy( self.ViewModelBoneMods )


                self:CreateModels(self.VElements) // create viewmodels
                self:CreateModels(self.WElements) // create worldmodels
                
                // init view model bone build function
                if IsValid(self.Owner) then
                        local vm = self.Owner:GetViewModel()
                        if IsValid(vm) then
                                self:ResetBonePositions(vm)
                                
                                // Init viewmodel visibility
                                if (self.ShowViewModel == nil or self.ShowViewModel) then
                                        vm:SetColor(Color(255,255,255,255))
                                else
                                        // we set the alpha to 1 instead of 0 because else ViewModelDrawn stops being called
                                        vm:SetColor(Color(255,255,255,1))
                                        // ^ stopped working in GMod 13 because you have to do Entity:SetRenderMode(1) for translucency to kick in
                                        // however for some reason the view model resets to render mode 0 every frame so we just apply a debug material to prevent it from drawing
                                        vm:SetMaterial("Debug/hsv")                        
                                end
                        end
                end
                
        end


end


function SWEP:Holster()
        
        if CLIENT and IsValid(self.Owner) then
                local vm = self.Owner:GetViewModel()
                if IsValid(vm) then
                        self:ResetBonePositions(vm)
                end
        end
        
        return true
end


function SWEP:OnRemove()
        self:Holster()
end


if CLIENT then


        SWEP.vRenderOrder = nil
        function SWEP:ViewModelDrawn()
                
                local vm = self.Owner:GetViewModel()
                if !IsValid(vm) then return end
                
                if (!self.VElements) then return end
                
                self:UpdateBonePositions(vm)


                if (!self.vRenderOrder) then
                        
                        // we build a render order because sprites need to be drawn after models
                        self.vRenderOrder = {}


                        for k, v in pairs( self.VElements ) do
                                if (v.type == "Model") then
                                        table.insert(self.vRenderOrder, 1, k)
                                elseif (v.type == "Sprite" or v.type == "Quad") then
                                        table.insert(self.vRenderOrder, k)
                                end
                        end
                        
                end


                for k, name in ipairs( self.vRenderOrder ) do
                
                        local v = self.VElements[name]
                        if (!v) then self.vRenderOrder = nil break end
                        if (v.hide) then continue end
                        
                        local model = v.modelEnt
                        local sprite = v.spriteMaterial
                        
                        if (!v.bone) then continue end
                        
                        local pos, ang = self:GetBoneOrientation( self.VElements, v, vm )
                        
                        if (!pos) then continue end
                        
                        if (v.type == "Model" and IsValid(model)) then


                                model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
                                ang:RotateAroundAxis(ang:Up(), v.angle.y)
                                ang:RotateAroundAxis(ang:Right(), v.angle.p)
                                ang:RotateAroundAxis(ang:Forward(), v.angle.r)


                                model:SetAngles(ang)
                                //model:SetModelScale(v.size)
                                local matrix = Matrix()
                                matrix:Scale(v.size)
                                model:EnableMatrix( "RenderMultiply", matrix )
                                
                                if (v.material == "") then
                                        model:SetMaterial("")
                                elseif (model:GetMaterial() != v.material) then
                                        model:SetMaterial( v.material )
                                end
                                
                                if (v.skin and v.skin != model:GetSkin()) then
                                        model:SetSkin(v.skin)
                                end
                                
                                if (v.bodygroup) then
                                        for k, v in pairs( v.bodygroup ) do
                                                if (model:GetBodygroup(k) != v) then
                                                        model:SetBodygroup(k, v)
                                                end
                                        end
                                end
                                
                                if (v.surpresslightning) then
                                        render.SuppressEngineLighting(true)
                                end
                                
                                render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
                                render.SetBlend(v.color.a/255)
                                model:DrawModel()
                                render.SetBlend(1)
                                render.SetColorModulation(1, 1, 1)
                                
                                if (v.surpresslightning) then
                                        render.SuppressEngineLighting(false)
                                end
                                
                        elseif (v.type == "Sprite" and sprite) then
                                
                                local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
                                render.SetMaterial(sprite)
                                render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
                                
                        elseif (v.type == "Quad" and v.draw_func) then
                                
                                local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
                                ang:RotateAroundAxis(ang:Up(), v.angle.y)
                                ang:RotateAroundAxis(ang:Right(), v.angle.p)
                                ang:RotateAroundAxis(ang:Forward(), v.angle.r)
                                
                                cam.Start3D2D(drawpos, ang, v.size)
                                        v.draw_func( self )
                                cam.End3D2D()


                        end
                        
                end
                
        end


        SWEP.wRenderOrder = nil
        function SWEP:DrawWorldModel()
                
                if (self.ShowWorldModel == nil or self.ShowWorldModel) then
                        self:DrawModel()
                end
                
                if (!self.WElements) then return end
                
                if (!self.wRenderOrder) then


                        self.wRenderOrder = {}


                        for k, v in pairs( self.WElements ) do
                                if (v.type == "Model") then
                                        table.insert(self.wRenderOrder, 1, k)
                                elseif (v.type == "Sprite" or v.type == "Quad") then
                                        table.insert(self.wRenderOrder, k)
                                end
                        end


                end
                
                if (IsValid(self.Owner)) then
                        bone_ent = self.Owner
                else
                        // when the weapon is dropped
                        bone_ent = self
                end
                
                for k, name in pairs( self.wRenderOrder ) do
                
                        local v = self.WElements[name]
                        if (!v) then self.wRenderOrder = nil break end
                        if (v.hide) then continue end
                        
                        local pos, ang
                        
                        if (v.bone) then
                                pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent )
                        else
                                pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent, "ValveBiped.Bip01_R_Hand" )
                        end
                        
                        if (!pos) then continue end
                        
                        local model = v.modelEnt
                        local sprite = v.spriteMaterial
                        
                        if (v.type == "Model" and IsValid(model)) then


                                model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
                                ang:RotateAroundAxis(ang:Up(), v.angle.y)
                                ang:RotateAroundAxis(ang:Right(), v.angle.p)
                                ang:RotateAroundAxis(ang:Forward(), v.angle.r)


                                model:SetAngles(ang)
                                //model:SetModelScale(v.size)
                                local matrix = Matrix()
                                matrix:Scale(v.size)
                                model:EnableMatrix( "RenderMultiply", matrix )
                                
                                if (v.material == "") then
                                        model:SetMaterial("")
                                elseif (model:GetMaterial() != v.material) then
                                        model:SetMaterial( v.material )
                                end
                                
                                if (v.skin and v.skin != model:GetSkin()) then
                                        model:SetSkin(v.skin)
                                end
                                
                                if (v.bodygroup) then
                                        for k, v in pairs( v.bodygroup ) do
                                                if (model:GetBodygroup(k) != v) then
                                                        model:SetBodygroup(k, v)
                                                end
                                        end
                                end
                                
                                if (v.surpresslightning) then
                                        render.SuppressEngineLighting(true)
                                end
                                
                                render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
                                render.SetBlend(v.color.a/255)
                                model:DrawModel()
                                render.SetBlend(1)
                                render.SetColorModulation(1, 1, 1)
                                
                                if (v.surpresslightning) then
                                        render.SuppressEngineLighting(false)
                                end
                                
                        elseif (v.type == "Sprite" and sprite) then
                                
                                local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
                                render.SetMaterial(sprite)
                                render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
                                
                        elseif (v.type == "Quad" and v.draw_func) then
                                
                                local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
                                ang:RotateAroundAxis(ang:Up(), v.angle.y)
                                ang:RotateAroundAxis(ang:Right(), v.angle.p)
                                ang:RotateAroundAxis(ang:Forward(), v.angle.r)
                                
                                cam.Start3D2D(drawpos, ang, v.size)
                                        v.draw_func( self )
                                cam.End3D2D()


                        end
                        
                end
                
        end


        function SWEP:GetBoneOrientation( basetab, tab, ent, bone_override )
                
                local bone, pos, ang
                if (tab.rel and tab.rel != "") then
                        
                        local v = basetab[tab.rel]
                        
                        if (!v) then return end
                        
                        // Technically, if there exists an element with the same name as a bone
                        // you can get in an infinite loop. Let's just hope nobody's that stupid.
                        pos, ang = self:GetBoneOrientation( basetab, v, ent )
                        
                        if (!pos) then return end
                        
                        pos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
                        ang:RotateAroundAxis(ang:Up(), v.angle.y)
                        ang:RotateAroundAxis(ang:Right(), v.angle.p)
                        ang:RotateAroundAxis(ang:Forward(), v.angle.r)
                                
                else
                
                        bone = ent:LookupBone(bone_override or tab.bone)


                        if (!bone) then return end
                        
                        pos, ang = Vector(0,0,0), Angle(0,0,0)
                        local m = ent:GetBoneMatrix(bone)
                        if (m) then
                                pos, ang = m:GetTranslation(), m:GetAngles()
                        end
                        
                        if (IsValid(self.Owner) and self.Owner:IsPlayer() and 
                                ent == self.Owner:GetViewModel() and self.ViewModelFlip) then
                                ang.r = -ang.r // Fixes mirrored models
                        end
                
                end
                
                return pos, ang
        end


        function SWEP:CreateModels( tab )


                if (!tab) then return end


                // Create the clientside models here because Garry says we can't do it in the render hook
                for k, v in pairs( tab ) do
                        if (v.type == "Model" and v.model and v.model != "" and (!IsValid(v.modelEnt) or v.createdModel != v.model) and 
                                        string.find(v.model, ".mdl") and file.Exists (v.model, "GAME") ) then
                                
                                v.modelEnt = ClientsideModel(v.model, RENDER_GROUP_VIEW_MODEL_OPAQUE)
                                if (IsValid(v.modelEnt)) then
                                        v.modelEnt:SetPos(self:GetPos())
                                        v.modelEnt:SetAngles(self:GetAngles())
                                        v.modelEnt:SetParent(self)
                                        v.modelEnt:SetNoDraw(true)
                                        v.createdModel = v.model
                                else
                                        v.modelEnt = nil
                                end
                                
                        elseif (v.type == "Sprite" and v.sprite and v.sprite != "" and (!v.spriteMaterial or v.createdSprite != v.sprite) 
                                and file.Exists ("materials/"..v.sprite..".vmt", "GAME")) then
                                
                                local name = v.sprite.."-"
                                local params = { ["$basetexture"] = v.sprite }
                                // make sure we create a unique name based on the selected options
                                local tocheck = { "nocull", "additive", "vertexalpha", "vertexcolor", "ignorez" }
                                for i, j in pairs( tocheck ) do
                                        if (v[j]) then
                                                params["$"..j] = 1
                                                name = name.."1"
                                        else
                                                name = name.."0"
                                        end
                                end


                                v.createdSprite = v.sprite
                                v.spriteMaterial = CreateMaterial(name,"UnlitGeneric",params)
                                
                        end
                end
                
        end
        
        local allbones
        local hasGarryFixedBoneScalingYet = false


        function SWEP:UpdateBonePositions(vm)
                
                if self.ViewModelBoneMods then
                        
                        if (!vm:GetBoneCount()) then return end
                        
                        // !! WORKAROUND !! //
                        // We need to check all model names :/
                        local loopthrough = self.ViewModelBoneMods
                        if (!hasGarryFixedBoneScalingYet) then
                                allbones = {}
                                for i=0, vm:GetBoneCount() do
                                        local bonename = vm:GetBoneName(i)
                                        if (self.ViewModelBoneMods[bonename]) then 
                                                allbones[bonename] = self.ViewModelBoneMods[bonename]
                                        else
                                                allbones[bonename] = { 
                                                        scale = Vector(1,1,1),
                                                        pos = Vector(0,0,0),
                                                        angle = Angle(0,0,0)
                                                }
                                        end
                                end
                                
                                loopthrough = allbones
                        end
                        // !! ----------- !! //
                        
                        for k, v in pairs( loopthrough ) do
                                local bone = vm:LookupBone(k)
                                if (!bone) then continue end
                                
                                // !! WORKAROUND !! //
                                local s = Vector(v.scale.x,v.scale.y,v.scale.z)
                                local p = Vector(v.pos.x,v.pos.y,v.pos.z)
                                local ms = Vector(1,1,1)
                                if (!hasGarryFixedBoneScalingYet) then
                                        local cur = vm:GetBoneParent(bone)
                                        while(cur >= 0) do
                                                local pscale = loopthrough[vm:GetBoneName(cur)].scale
                                                ms = ms * pscale
                                                cur = vm:GetBoneParent(cur)
                                        end
                                end
                                
                                s = s * ms
                                // !! ----------- !! //
                                
                                if vm:GetManipulateBoneScale(bone) != s then
                                        vm:ManipulateBoneScale( bone, s )
                                end
                                if vm:GetManipulateBoneAngles(bone) != v.angle then
                                        vm:ManipulateBoneAngles( bone, v.angle )
                                end
                                if vm:GetManipulateBonePosition(bone) != p then
                                        vm:ManipulateBonePosition( bone, p )
                                end
                        end
                else
                        self:ResetBonePositions(vm)
                end
                   
        end
         
        function SWEP:ResetBonePositions(vm)
                
                if (!vm:GetBoneCount()) then return end
                for i=0, vm:GetBoneCount() do
                        vm:ManipulateBoneScale( i, Vector(1, 1, 1) )
                        vm:ManipulateBoneAngles( i, Angle(0, 0, 0) )
                        vm:ManipulateBonePosition( i, Vector(0, 0, 0) )
                end
                
        end


        /**************************
                Global utility code
        **************************/


        // Fully copies the table, meaning all tables inside this table are copied too and so on (normal table.Copy copies only their reference).
        // Does not copy entities of course, only copies their reference.
        // WARNING: do not use on tables that contain themselves somewhere down the line or you'll get an infinite loop
        function table.FullCopy( tab )


                if (!tab) then return nil end
                
                local res = {}
                for k, v in pairs( tab ) do
                        if (type(v) == "table") then
                                res[k] = table.FullCopy(v) // recursion ho!
                        elseif (type(v) == "Vector") then
                                res[k] = Vector(v.x, v.y, v.z)
                        elseif (type(v) == "Angle") then
                                res[k] = Angle(v.p, v.y, v.r)
                        else
                                res[k] = v
                        end
                end
                
                return res
                
        end
        
end


The console reports that the angles are changing, but the VElements are NOT updating

https://dl.dropboxusercontent.com/u/888382/anglefucker.png

I can’t for the life of me figure out why the angles aren’t updating.
I did a weird test where I changed SWEP:Think() to



function SWEP:Think()
self.VElements["GearSmall"].angle.y = self.Gears_Angle
self.Gears_Angle = self.Gears_Angle + 1
end


and somehow that makes it spin

https://dl.dropboxusercontent.com/u/888382/withself.gif

I have no idea what the christ is going on

Its printing that in blue meaning its serverside… you’ll need it running clientside to update view model.

Strange you have a different result with your second example, unless you were testing one in multiplayer and one in single player or something.

They’re both ran in singleplayer.
So what does that mean, should the variable be declared both server and clientside? Or does that mean I have to make these gear values local variables?

This means you gotta make the variables clientside to actually work. Try testing it in multiplayer, I have a feeling that most SWEP functions aren’t called clientside in singleplayer.

[editline]6th December 2013[/editline]

I think SWEP:PrimaryAttack does this, because there’s no prediction in single player. So, you gotta do self:CallOnClient(“SetMyValues”, “1 0.1”) from SWEP:PrimaryAttack and set the values from SWEP:SetMyValues.

That doesn’t appear to be the issue though - The image showed that the variables WERE changing. making them local now fucks up the variables altogether.

The variables all updated fine - what was the problem is that the gear fails to spin. I wanted to check wether it was the variables fucking up or the SWEP construction kit’s code by adding


MsgN( "Y value:" ..tostring(self.VElements["GearSmall"].angle.y))

to SWEP:PrimaryAttack.
This reports back that, in the table, it’s yaw value is changing. The vector in that table is definitely changing. It’s just not updating for that angle.

It’s just really odd how that fucks up yet by simply doing


self.VElements["GearSmall"].angle.y = self.Gears_Angle
self.Gears_Angle = self.Gears_Angle + 1

works perfectly fine.

The code in the OP is the code i’m working with - you can check this out for yourself.

I never said the vars weren’t changing or making them local, what I am saying is that in singleplayer, SWEP:PrimaryAttack doesn’t get called on client, meaning this piece of code:


self.Gears_AngularVelocity = 1
    self.Gears_AngularAcceleration = -0.1

is never called on client, resulting in those variables not updating on client. And self.VElements only makes effect on client, not server.

I see now. So i’d have to essentially repeat the function twice, once in SWEP:PrimaryAttack and then calling the function callonclient in SWEP:PrimaryAttack?

if ( game.SinglePlayer() ) then self:CallOnClient(“PrimaryAttack”) end

that should do the trick.

Wait, so you are calling the primary attack function ON the client? I’m assuming this isn’t placed within the SWEP:PrimaryAttack function as that would cause a loop wouldnt it?

It won’t loop. It runs on the server, the server tells the client to run it. End of loop.

[lua]
function SWEP:PrimaryAttack()
if SERVER and game.SinglePlayer() then
self:CallOnClient(‘PrimaryAttack’,’’)
end
if CLIENT then
–update variables
end
end
[/lua]

Works like a charm. Thank you very much for your help

The bastard in action:

https://dl.dropboxusercontent.com/u/888382/yay.gif