"attempt to index global 'SWEP' (a nil value)." I've google searched and I couldn't find a solution

I’m trying to make a weapon for my TTT server but I’m getting this


[ERROR] lua/weapons/weapon_launchcannon.lua:17: attempt to index global 'SWEP' (a nil value)
  1. unknown - lua/weapons/weapon_launchcannon.lua:17


I’ve searched google over and over and looked at weapon_fists.lua and the weapon base but I have no idea why I’m getting this error. I’m loading it via “lua_openscript weapons/weapon_launchcannon.lua”

Here’s the code


if SERVER then
   AddCSLuaFile()
   resource.AddFile("materials/VGUI/ttt/icon_destroyer_launchcannon.vmt")
end

if CLIENT then
   SWEP.PrintName = "Launch Cannon"
   SWEP.Slot      = 7 -- add 1 to get the slot number key

   SWEP.ViewModelFOV  = 72
   SWEP.ViewModelFlip = true
end

-- Always derive from weapon_tttbase.

--- Standard GMod values
SWEP.Base				= "weapon_base"
SWEP.Primary.Automatic   = true
SWEP.Primary.Damage      = 0
SWEP.Primary.Ammo        = "HelicopterGun"

SWEP.Primary.NumShots	= 0
SWEP.Primary.Delay 		= 1
SWEP.Primary.ClipSize    = 1
SWEP.Primary.ClipMax     = 1

SWEP.Primary.DefaultClip = 1
SWEP.ViewModel  = "models/weapons/c_rpg.mdl"
SWEP.WorldModel = "models/weapons/w_rocket_launcher.mdl"




function SWEP:Initialize()
	self:SetHoldType("rpg")
	end
	
	
--- TTT config values

-- Kind specifies the category this weapon is in. Players can only carry one of
-- each. Can be: WEAPON_... MELEE, PISTOL, HEAVY, NADE, CARRY, EQUIP1, EQUIP2 or ROLE.
-- Matching SWEP.Slot values: 0      1       2     3      4      6       7        8
SWEP.Kind = WEAPON_EQUIP2

-- If AutoSpawnable is true and SWEP.Kind is not WEAPON_EQUIP1/2, then this gun can
-- be spawned as a random weapon. Of course this AK is special equipment so it won't,
-- but for the sake of example this is explicitly set to false anyway.
SWEP.AutoSpawnable = false

-- The AmmoEnt is the ammo entity that can be picked up when carrying this gun.

-- CanBuy is a table of ROLE_* entries like ROLE_TRAITOR and ROLE_DETECTIVE. If
-- a role is in this table, those players can buy this.
SWEP.CanBuy = { ROLE_TRAITOR, ROLE_DETECTIVE }


-- InLoadoutFor is a table of ROLE_* entries that specifies which roles should
-- receive this weapon as soon as the round starts. In this case, none.
SWEP.InLoadoutFor = nil

-- If LimitedStock is true, you can only buy one per round.
SWEP.LimitedStock = false

-- If NoSights is true, the weapon won't have ironsights
SWEP.NoSights = true

-- Equipment menu information is only needed on the client
if CLIENT then
   -- Path to the icon material
   SWEP.Icon = "VGUI/ttt/icon_destroyer_launchcannon.vmt"

   -- Text shown in the equip menu
   SWEP.EquipMenuData = {
      type = "Launch Cannon",
      desc = "Aim at the ground to launch yourself!"
   };
end

function SWEP:shoot()
	local tr = self.Owner:GetEyeTrace()
	self:EmitSound(Sound("weapons/grenade_launcher1.wav"))
	
	if (!SERVER) then return end
	
	local ent = ents.Create("prop_physics")
	ent:SetModel("props_junk/PopCan01a.mdl")
	ent:SetPos(self.Owner:EyePos() + (self.OwnerGetAimVector()* 16))
	ent:SetAngles(self.Owner:EyeAngles())
	ent:Spawn()
	
	
	
	local phys = ent:GetPhysicsObject()
	if !(phys && IsValid(phys)) then ent:Remove() return end
	
	phys:ApplyForceCenter(self.Owner:GetAimVector():GetNormalized()* math.pow(tr.HitPos:Length(), 3))
	
	
end

function SWEP:PrimaryAttack()
		self:shoot()
	end


	
	
	
	
	
function SWEP:Think()
end
function SWEP:Reload()
end
-- Tell the server that it should download our icon to clients.


Loading it with lua_openscript will break it. You need to put it in the weapons folder, restart the server/game and autorefresh should let you keep editing it while reloading anything you do to it.

thanks, but is there a reason as to why lua_openscript doesn’t work? or is it broken in itself

lua_openscript doesn’t set proper context (the ENT/SWEP/etc. variables) for stuff, it’s effectively doing a RunString() on file contents.