I feel stupid.

I feel absolutely retarded, I have been making a three-way SWEP for garrys mod as three way sweps are more stable/sturdy but I had’nt managed to make it multiplayer compatible. The three way swep works perfectly in single player but as I foolishly used GetPlayerById it couldn’t possibly work in multiplayer.

My problem is the one-script SWEP does not show up on my Q menu, Nor can I give it to myself through the console.
C:\Program Files (x86)\Steam\steamapps\common\GarrysMod\garrysmod\addons\Deception\lua\weapons\Deceptions_Force

Three way swep:
Shared.lua


SWEP.Author = " Deception"
SWEP.Contact = "Private"
SWEP.Purpose = "Use the force luke."
SWEP.Instructions = "Left-Click to move an object, Right-Click to scroll through force abilities."
 
SWEP.Category = "Deception"

SWEP.Spawnable = true -- Whether regular players can see it
SWEP.AdminOnly = false

SWEP.ViewModel = "models/weapons/v_stunbaton.mdl" 
SWEP.WorldModel = "models/weapons/w_stunbaton.mdl" 



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


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


local lastReloadTime=0;
function SWEP:Reload()

reloadtime = 0
    if ((os.time()-lastReloadTime)>1) then
        if reloadtime == 0 then
			reloadtime = 1
			forcereload(self.Owner)
			
            
        elseif reloadtime == 1 then
            reloadtime = 0
			
            
        end
        lastReloadTime=os.time() 
    end
end



 
function SWEP:Think()

end
function SWEP:PrimaryAttack()
	forceprimary(self.Owner)
end
 
function SWEP:SecondaryAttack()
	forcesecondary(self.Owner)
end
	

   

 

Init.lua


AddCSLuaFile ("cl_init.lua")
AddCSLuaFile ("shared.lua")

include ("shared.lua")

SWEP.HoldType = "knife" 
SWEP.Base = "weapon_base"

SWEP.Weight = 5

SWEP.AutoSwitchTo = false
SWEP.AutoSwitchFrom = false

util.AddNetworkString("forcesecondary")
util.AddNetworkString("forceprimary")
util.AddNetworkString("forcereload")





function forcesecondary(ply)
    local pr = Entity( 1 ):GetEyeTrace().Entity
	      local pl = player.GetByID( 1 )
	      local en = Entity( 1 ):GetEyeTrace().Entity
		  local ms = pr:GetPhysicsObject():GetMass()
          en:SetVelocity( pl:GetAimVector() * 3000 + Vector(0,0,300))
	      pr:GetPhysicsObject():ApplyForceCenter(pl:GetAimVector() * 1 * 10 * ms  + Vector(0,0,ms * 2))
end
forceability = 1
function forcereload(ply)
    local pl = player.GetByID( 1 )
    forceability = forceability + 1
    if forceability == 4 then
	forceability = 1
	end
	
	 if forceability == 1 then
	player.GetByID( 1 ):ChatPrint( "Force Leap" )
	
       
    elseif forceability == 2 then
	player.GetByID( 1 ):ChatPrint("Force Choke")
	
	elseif forceability == 3 then
	player.GetByID(1):ChatPrint("Force Push")

        end
  	

end
 

function forceprimary(ply)
    if forceability == 1 then
	
	
	
	   if Entity(1):IsOnGround() == true then 
	
	      local pl = player.GetByID( 1 )
          pl:SetVelocity( pl:GetAimVector() * 1250 )
		  end
       
    elseif forceability == 2 then
	
	elseif forceability == 3 then
	      local pr = Entity( 1 ):GetEyeTrace().Entity
	      local pl = player.GetByID( 1 )
	      local en = Entity( 1 ):GetEyeTrace().Entity
		  local ms = pr:GetPhysicsObject():GetMass()
          en:SetVelocity( pl:GetAimVector() * 3000 + Vector(0,0,300))
	      pr:GetPhysicsObject():ApplyForceCenter(pl:GetAimVector() * 1 * 800 * ms  + Vector(0,0,ms * 500))
		
	

           end
	
end


cl_init.lua


include ("shared.lua")
AddCSLuaFile( "init.lua" )

SWEP.PrintName = "StarWars RolePlay Force Swep"
SWEP.Slot = 4
SWEP.SlotPos = 1
SWEP.DrawAmmo = false
SWEP.DrawCrosshair = true






And this is my one script version that will not work at all.
Again I apologise for both my sloppy untidy code and the fact this is most likely a simple mistake, I am using this swep to learn glua mainly.


AddCSLuaFile ()

if SERVER then 
 
	
	
 
	
	SWEP.Weight = 5
    SWEP.AutoSwitchTo = false
	SWEP.AutoSwitchFrom = false
 
elseif CLIENT then 
 


SWEP.PrintName = "StarWars RolePlay Force Swep"
SWEP.Slot = 4
SWEP.SlotPos = 1
SWEP.DrawAmmo = false
SWEP.DrawCrosshair = true

end





SWEP.Author = " Deception"
SWEP.Contact = "Private"
SWEP.Purpose = "Use the force luke."
SWEP.Instructions = "Left-Click to move an object, Right-Click to scroll through force abilities."
 
SWEP.Category = "Deceptional"

SWEP.Spawnable = true -- Whether regular players can see it
SWEP.AdminOnly = false

SWEP.ViewModel = "models/weapons/v_stunbaton.mdl" 
SWEP.WorldModel = "models/weapons/w_stunbaton.mdl" 



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


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




local own = self:GetOwner()
local lastReloadTime=0;

function SWEP:Reload()
forceability = 1
reloadtime = 0
    if ((os.time()-lastReloadTime)>1) then
        if reloadtime == 0 then
			reloadtime = 1
			 if reloadtime == 1 then
            reloadtime = 0
		end
            

	local pl = own
    forceability = forceability + 1
      if forceability == 4 then
	    forceability = 1
	end
	
    if (CLIENT) then 
	    print("Client works kek")
	elseif (SERVER) then
	    if forceability == 1 then
	   own:ChatPrint( "Force Leap" )
	
       
       elseif forceability == 2 then
	   own:ChatPrint("Force Choke")
	
	   elseif forceability == 3 then
	   own:ChatPrint("Force Push")
	end
	
     if reloadtime == 1 then
        reloadtime = 0
			
            
        end
       lastReloadTime=os.time()
    
	  
  	

			
            
    
    end
   end  
  end
 end



 
function SWEP:Think()

end
function SWEP:PrimaryAttack()
  if (CLIENT) then 
    print("clientworks")
	
  
 elseif (SERVER) then
	 if forceability == 1 then
	
	
	
	   if Entity(1):IsOnGround() == true then 
	
	      local pl = own
          pl:SetVelocity( pl:GetAimVector() * 1250 )
		  end
       
    elseif forceability == 2 then
	
	elseif forceability == 3 then
	      local pr = own:GetEyeTrace().Entity
	      local pl = own
	      local en = Entity( 1 ):GetEyeTrace().Entity
		  local ms = pr:GetPhysicsObject():GetMass()
          en:SetVelocity( pl:GetAimVector() * 3000 + Vector(0,0,300))
	      pr:GetPhysicsObject():ApplyForceCenter(pl:GetAimVector() * 1 * 800 * ms  + Vector(0,0,ms * 500))
		  
  end
 end
end


function SWEP:SecondaryAttack()
          if (CLIENT) then
		  
		  
		  elseif (SERVER) then
		     local pr = own:GetEyeTrace().Entity
	         local pl = own
	         local en = own:GetEyeTrace().Entity
		     local ms = pr:GetPhysicsObject():GetMass()
             en:SetVelocity( pl:GetAimVector() * 3000 + Vector(0,0,300))
	         pr:GetPhysicsObject():ApplyForceCenter(pl:GetAimVector() * 1 * 10 * ms  + Vector(0,0,ms * 2))
  end
 end		  
	

   

 

Also despite the SWEP name, This is not a scriptfodder job I signed up for without knowing shit :3
All feedback is appreciated, Thanks!

Bump for trump?

Honestly, people just look on your code and go back to the forums, it’s just too much fr the eye :S

As a developer, I’d suggest you the simplest method, ie. get a basic (“skeleton”) TTT SWEP that has no features, but works and shows up in the credit menu.

Then, systematically, implement your functions and variables, one-by-one (that is, structure-by-structure), and see at which point it fails. You can then debug that very structure to see exactly what’s going on. (Unless you’re in a hurry, don’t be flattered when suddenly your code works,** find out why it works**, or you’ll never learn.)

Although this is a time consuming way of doing it, without someone knowing the frequent-causes-that-mess-your-code-up book relevant here, there are no common spots and mistakes to check. It’s probably a missing variable or something, really, really stupid, and don’t worry it often goes down on stupid things like that, it’s not quite your fault - though I’m barely familiar with custom TTT SWEPs tbh, so can’t help too much ^_^"

Good luck, and let us know what was the issue if you happen to find it!

Thourght that was the problem ‘too much for the eye’…
And thanks!

I skimmed over your code, there’s actually a lot of things wrong with it. I don’t want to overwhelm you so here’s a few of them:

[lua]local own = self:GetOwner()[/lua]
This doesn’t work. Don’t look for shortcuts before you understand them - use self:GetOwner() or self.Owner every time.

[lua]function SWEP:Reload()

reloadtime = 0
if ((os.time()-lastReloadTime)>1) then
if reloadtime == 0 then
reloadtime = 1
forcereload(self.Owner)

    elseif reloadtime == 1 then
        reloadtime = 0
		
        
    end
    lastReloadTime=os.time() 
end

end[/lua]
reloadtime is always going to be 0, because you just set it to 0 at the top of the function. Also, you shouldn’t be using global variables.

Even worse - this function calls the global function forcereload (I don’t know why). This global function is only defined on the server, not the client, so it will give you an error clientside. This doesn’t happen in singleplayer because SWEP:Reload isn’t called clientside in singleplayer, but it is in multiplayer.

There’s more, but first you should understand those two things and fix them.

There really isn’t much wrong with doing this at the start of a function or wherever you need it.
[lua]local owner = self:GetOwner()[/lua]
In fact it’s actually a micro-optimization so you don’t have to constantly keep calling Entity.GetOwner()

I fixed the reloadtime = 0 thing before you commented but thanks anyways. Also Ill look into it.

[editline]18th August 2016[/editline]

Yeah I thourght so, Although it did say self was a nil value although Ill see what I can do?

Because you’re doing it outside of the functions, self only exists inside of them.

Oh, Thanks a bunch, Now to find out why the swep category isn’t even showing up :3

Where are you placing the one script version and what is it named?

As I stated ‘C:\Program Files (x86)\Steam\steamapps\common\GarrysMod\garrysmod\a ddons\Deception\lua\weapons\Deceptions_Force’, Also it’s called shared.lua.

When making a single file weapon you don’t use the subfolder.
e.g. “gmod/addons/Deception/lua/weapons/Deceptions_Force.lua”

Ah Thanks.

-snip- Fixed it, Thank you everyone for your contributions ;D <3