SWEP Doesnt work! Help!

So i’m new to all this lua coding world, literally my 5th Day of learning, but i came across this page…from a website that shows you how to make your first SWEP
So i read everything, made sure i understood everything, and did my own SWEP! Everything was great till i spawned in the game…
Here is My Code
[lua]

if SERVER then

AddCSLuaFile (shared.lua)

SWEP.Weight = 7 

SWEP.AutoSwitchTo = false 
SWEP.AutoSwitchFrom = false 

elseif CLIENT then

SWEP.PrintName = "Chair Thrower"

SWEP.Slot = 4
SWEP.SlotPos = 3 

SWEP.DrawAmmo = false 
SWEP.DrawCrosshair = false 

language.Add("Undone_Thrown_Chair","Undone Thrown Chair")

end

SWEP.Author = "Cicada 3301"
SWEP.Contact = "https://facepunch.com/member.php?u=740356"
SWEP.Purpose = "Shoots Some CHAIRS!"
SWEP.Instructions = "Left Click to Shoot - Refill with Pistol Ammo"

SWEP.Category = "Cicadas SWEPS!"

SWEP.Spawnable = true
SWEP.AdminSpawnable = true

SWEP.ViewModel = "models/weapons/v_RPG.mdl" 
SWEP.WorldModel = "models/weapons/w_rocket_launcher.mdl"

SWEP.Primary.ClipSize = -1 	

SWEP.Primary.DefaultClip = -1

SWEP.Primary.Automatic = false 

SWEP.Primary.Ammo = "Pistol"

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

local ShootSound = Sound("Metal.SawbladeStick")

function SWEP:Reload()
end

function SWEP:Think()
end

function SWEP:throw_attack (model_file)
local tr = self.Owner:GetEyeTrace()

if (!SERVER) then return end

local ent = ents.Create("models/props_c17/FurnitureChair001a.mdl")
ent:SetModel(model_file)

ent:SetPos(self.Owner:EyePos() + (self.Owner:GetAimVector() * 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))



cleanup.Add(self.Owner, "props", ent)

undo.Create ("Thrown_Chair")
	undo.AddEntity (ent)
	undo.SetPlayer (self.Owner)
undo.Finish()

end

function SWEP:PrimaryAttack()
self:throw_attack(“models/props/cs_office/Chair_office.mdl”)
end

function SWEP:SecondaryAttack()

self:throw_attack("models/props_c17/FurnitureChair001a.mdl")

end
[/lua]
And i get this Error message whenever i attempt to shoot “Attempting to Index Global SWEP [A Nil Value]”
I Did try to Reset the game, see for any line errors, any typos, nothing…
Then when i tried the Code they provided at the Website

Which was this
VVVVVVVVV

[lua]
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 = "Chair throwing gun"

//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 = 4
SWEP.SlotPos = 1

//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

//Ensures a clean looking notification when a chair is undone. How it works:
//When you create an undo, you specify the ID:
//		undo.Create("Some_Identity")
//By creating an associated language, we can make the undo notification look better:
//		language.Add("Undone_Some_Identity", "Some message...")

language.Add("Undone_Thrown_SWEP_Entity","Undone Thrown SWEP Entity")

end

SWEP.Author = “Your Name”
SWEP.Contact = “Your Email Address”
SWEP.Purpose = “What your SWep does.”
SWEP.Instructions = “How to operate your SWep”

//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 = “Category”

SWEP.Spawnable = true – Whether regular players can see it
SWEP.AdminSpawnable = true – Whether Admins/Super Admins can see it

SWEP.ViewModel = “models/weapons/v_RPG.mdl” – This is the model used for clients to see in first person.
SWEP.WorldModel = “models/weapons/w_rocket_launcher.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”

//When the script loads, the sound ‘‘Metal.SawbladeStick’’ will be precached,
//and a local variable with the sound name created.
local ShootSound = Sound(“Metal.SawbladeStick”)

function SWEP:Reload()
end

function SWEP:Think()
end

function SWEP:throw_attack (model_file)
//Get an eye trace. This basically draws an invisible line from
//the players eye. This SWep makes very little use of the trace, except to
//calculate the amount of force to apply to the object thrown.
local tr = self.Owner:GetEyeTrace()

//Play some noises/effects using the sound we precached earlier
self:EmitSound(ShootSound)
self.BaseClass.ShootEffects(self)

//We now exit if this function is not running serverside
if (!SERVER) then return end

//The next task is to create a physics prop based on the supplied model
local ent = ents.Create("prop_physics")
ent:SetModel(model_file)

//Set the initial position and angles of the object. This might need some fine tuning;
//but it seems to work for the models I have tried.
ent:SetPos(self.Owner:EyePos() + (self.Owner:GetAimVector() * 16))
ent:SetAngles(self.Owner:EyeAngles())
ent:Spawn()

//Now we need to get the physics object for our entity so we can apply a force to it
local phys = ent:GetPhysicsObject()

//Check if the physics object is valid. If not, remove the entity and stop the function
if !(phys && IsValid(phys)) then ent:Remove() return end

//Time to apply the force. My method for doing this was almost entirely empirical 
//and it seems to work fairly intuitively with chairs.
phys:ApplyForceCenter(self.Owner:GetAimVector():GetNormalized() *  math.pow(tr.HitPos:Length(), 3))

//Now for the important part of adding the spawned objects to the undo and cleanup lists.
cleanup.Add(self.Owner, "props", ent)

undo.Create ("Thrown_SWEP_Entity")
	undo.AddEntity (ent)
	undo.SetPlayer (self.Owner)
undo.Finish()

end

//Throw an office chair on primary attack
function SWEP:PrimaryAttack()
//Call the throw attack function, with the office chair model
self:throw_attack(“models/props/cs_office/Chair_office.mdl”)
end

//Throw a wooden chair on secondary attack
function SWEP:SecondaryAttack()
//Call the throw attack function, this time with the wooden chair model
self:throw_attack(“models/props_c17/FurnitureChair001a.mdl”)
end

[/lua]

It works normally? Sorry if this is a stupid question/I’ll get banned for it, i’m really really new here, my first post ever… Thanks in Advance!


AddCSLuaFile (shared.lua)

You have forgotten the ".


AddCSLuaFile("shared.lua")

And post the full error.

Oh well thanks for letting me know that i forgot that code!
Buuuut!..well i got this error when i added the "

Tried to use a NULL entity!

  1. SetModel - [C]:-1
  2. throw_attack - lua/weapons/shared.lua:64
    3. unknown - lua/weapons/shared.lua:95

It lead me to this Code
At line 64,
[lua]
ent:SetModel(model_file)
[/lua]

To be honest with you, i don’t even know why i put this code, i just followed that tutorial, but this is all about the error part :stuck_out_tongue: Thanks in Advance!

Change


ents.Create("models/props_c17/FurnitureChair001a.mdl")

to


ents.Create("prop_physics")

Honestly if you’re struggling this hard with one of the most basic Glua tutorials, you’re going to struggle to learn. As most people pointed out, you have missed very minute things in the code such as “” which are obviously very important.

Personally I would recommend redoing the tutorial and making sure you get everything right. Oh and try to learn what the code actually does, IE google different things you don’t understand and learn why that bit of code is there.

Just what I would do personally.

Everything is Basic to someone who knows the code but advanced to someone who doesn’t, Thats why there is a forum for questions.

[editline]9th October 2016[/editline]

Thank you man, You keep pointing simple mistakes that i do, appreciate it

Also, At Line 66 SetPos(self.Owner:EyePost() + (self.Owner:GetAimVectory() * 16))
I Added * 16 in the SetModel, instead of adding a SetPos , hope it works…

Will update you guys.

[editline]9th October 2016[/editline]

Yess! Everything works now, i’ll just have to edit how fast it throws the chairs, Thanks markusmarkusz!

[editline]9th October 2016[/editline]

I Cant change the Title to Fixed, please dont ban me…

[editline]9th October 2016[/editline]

Well just got a new Error

[ERROR] lua/weapons/shared.lua:74: attempt to call method ‘length’ (a nil value)

  1. throw_attack - lua/weapons/shared.lua:74
  2. unknown - lua/weapons/shared.lua:94

When i wrote this Code
[lua]
if SERVER then

AddCSLuaFile ("shared.lua")

SWEP.Weight = 7 

SWEP.AutoSwitchTo = false 
SWEP.AutoSwitchFrom = false 

elseif CLIENT then

SWEP.PrintName = "Chair Thrower"

SWEP.Slot = 4
SWEP.SlotPos = 3 

SWEP.DrawAmmo = false 
SWEP.DrawCrosshair = false 

language.Add("Undone_Thrown_Chair","Undone Thrown Chair")

end

SWEP.Author = "Cicada 3301"
SWEP.Contact = "https://facepunch.com/member.php?u=740356"
SWEP.Purpose = "Shoots Some CHAIRS!"
SWEP.Instructions = "Left Click to Shoot - Refill with Pistol Ammo"

SWEP.Category = "Cicadas SWEPS!"

SWEP.Spawnable = true
SWEP.AdminSpawnable = true

SWEP.ViewModel = "models/weapons/v_RPG.mdl" 
SWEP.WorldModel = "models/weapons/w_rocket_launcher.mdl"

SWEP.Primary.ClipSize = -1 	

SWEP.Primary.DefaultClip = -1

SWEP.Primary.Automatic = false 

SWEP.Primary.Ammo = "Pistol"

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

local ShootSound = Sound("Metal.SawbladeStick")

function SWEP:Reload()
end

function SWEP:Think()
end

function SWEP:throw_attack (model_file)
local tr = self.Owner:GetEyeTrace()

if (!SERVER) then return end

local ent = ents.Create("prop_physics")
ent:SetModel(model_file)

ent:SetPos(self.Owner:EyePos() + (self.Owner:GetAimVector() * 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))

cleanup.Add(self.Owner, "Thrown_Chair", ent)

undo.Create ("Thrown_Chair")
	undo.AddEntity (ent)
	undo.SetPlayer (self.Owner)
undo.Finish()

end

function SWEP:PrimaryAttack()
self:throw_attack(“models/props/cs_office/Chair_office.mdl”)
end
[/lua]

[editline]9th October 2016[/editline]

Just fixed the SPACING, And IT WORKS FINALLY :smiley: Been TWO DAYS WORKING ON IT!

Where did you save it?