SWEP Problem (Nooby question)

Hi everyone,

I’m new to making SWEPs.
I copied this code off the wiki, and yes, I did read the tutorial properly. However, I get this error:

[ERROR] lua/autorun/slowmogun/shared.lua:7: attempt to index local ‘SWEP’ (a nil value)

I did look at the code myself, I tried changing ‘SWEP’ to ‘self’ but that didn’t work, I also tried making a variable; SWEP = self.

Here’s the full script:


if (SERVER) then 
 
	AddCSLuaFile ("shared.lua")
 
	self.Weight = 5
 
	self.AutoSwitchTo = false
	self.AutoSwitchFrom = false
 
elseif (CLIENT) then 
 
	self.PrintName = "Matrix"
 
	self.Slot = 1
	self.SlotPos = 4
 
	self.DrawAmmo = false
 
	self.DrawCrosshair = false
end
 
self.Author = "YaYaBinks3"
self.Contact = ""
self.Purpose = "Dodge bullets in slow motion action! :DD"
self.Instructions = "Left Click: Enter the Matrix; Right Click: Leave the Matrix"

self.Category = "YaYaBinks3's Mods"
 
self.Spawnable = false
self.AdminSpawnable = true

self.Primary.ClipSize = -1

self.Primary.DefaultClip = -1

self.Primary.Automatic = false

self.Primary.Ammo = "none"
 
self.Secondary.ClipSize = -1
self.Secondary.DefaultClip = -1
self.Secondary.Automatic = false
self.Secondary.Ammo = "none"
 
function self:Reload()
end
 
function self:Think()
end
 
function self:PrimaryAttack()
timer.Simple(0, game.ConsoleCommand, "host_timescale 0.10
") 
timer.Simple(0, game.ConsoleCommand, "pp_motionblur 1
")
end

function self:SecondaryAttack()
timer.Simple(0, game.ConsoleCommand, "host_timescale 1
") 
timer.Simple(0, game.ConsoleCommand, "pp_motionblur 0
")
end

Also, where should I put my SWEP? I currently have it in autorun\slowmogun (slowmogun being my directory.)

I ran the script through lua_openscript autorun\slowmogun, and that outputs the error.

Thanks everyone.

You put SWEPs into lua/weapons/<weapon class name>/

So for example, if you want to make a weapon with a classname of “swep_derpgun” you should create a garrysmod/lua/weapons/swep_derpgun/ folder and put all related files in there.

Don’t change SWEP to self, that’s not how it works.

Yeah, I did some research and I tried your way and it still doesn’t work. I remade it at my friend’s house, it works at his, but not at mine.

[editline]11th June 2013[/editline]

Here’s the new code:


if SERVER then // This is where the init.lua stuff goes.
 
	//This makes sure clients download the file
	AddCSLuaFile ("shared.lua")
 
	//How heavy the SWep is
	SWEP.Weight = 5
 
	//Allow automatic switching to/from this weapon when weapons are picked up
	SWEP.AutoSwitchTo = false
	SWEP.AutoSwitchFrom = false
 
elseif CLIENT then // This is where the cl_init.lua stuff goes
 
	//The name of the SWep, as appears in the weapons tab in the spawn menu(Q Menu)
	SWEP.PrintName = "The Matrix"
 
	//Sets the position of the weapon in the switching menu 
	//(appears when you use the scroll wheel or keys 1-6 by default)
	SWEP.Slot = 2
	SWEP.SlotPos = 4
 
	//Sets drawing the ammuntion levels for this weapon
	SWEP.DrawAmmo = false
 
	//Sets the drawing of the crosshair when this weapon is deployed
	SWEP.DrawCrosshair = false
	
end

mode = false

SWEP.Author = "YaYaBinks3"
SWEP.Contact = ""
SWEP.Purpose = "Enter the Matrix! Dodge bullets in slow motion."
SWEP.Instructions = "Left Click: Enter - Right Click/Reselect tool: Exit"
 
//The category that you SWep will be shown in, in the Spawn (Q) Menu 
//(This can be anything, GMod will create the categories for you)
SWEP.Category = "YaYaBinks3's Mods"
 
SWEP.Spawnable = false -- Whether regular players can see it
SWEP.AdminSpawnable = true -- Whether Admins/Super Admins can see it
 
SWEP.ViewModel = "models/weapons/v_hands.mdl" -- This is the model used for clients to see in first person.
SWEP.WorldModel = "models/weapons/w_hands.mdl" -- This is the model shown to all other clients and in third-person.
 
 
//This determins how big each clip/magazine for the gun is. You can 
//set it to -1 to disable the ammo system, meaning primary ammo will 
//not be displayed and will not be affected.
SWEP.Primary.ClipSize = -1
 
//This sets the number of rounds in the clip when you first get the gun. Again it can be set to -1.
SWEP.Primary.DefaultClip = -1
 
//Obvious. Determines whether the primary fire is automatic. This should be true/false
SWEP.Primary.Automatic = false
 
//Sets the ammunition type the gun uses, see below for a list of types.
SWEP.Primary.Ammo = "none"
 
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"
 
function SWEP:Reload()
end
 
function SWEP:Think()
end
 
 
function SWEP:throw_attack (typex)
if typex == true then
mode = true
game.SetTimeScale(0.1)
RunConsoleCommand( "pp_motionblur", "1" )
elseif typex == false then
game.SetTimeScale(1)
RunConsoleCommand( "pp_motionblur", "0" )
end
end
 
 
//Throw an office chair on primary attack
function SWEP:PrimaryAttack()
	self:throw_attack(true)
end
 
//Throw a wooden chair on secondary attack
function SWEP:SecondaryAttack()
	self:throw_attack(false)
end

You can’t localize SWEP like that. It has to be Global.

It is global. Why would it be local? I haven’t overwrited it.

Sorry had this tab open for a while and didn’t refresh.

Don’t use the old GMOD wiki’s tutorials on SWEPs, they’re complete crap.

Look at this instead.
http://glua.me/bin/?path=/gamemodes/base/entities/weapons/weapon_base/shared.lua

Ah, that’s better. I’ll try that soon.