How to fix: 'attempt to index global 'SWEP' (a nil value)'?

I can post code if needed. Ok, bad.



if SERVER then AddCSLuaFile("shared.lua") end
SWEP.Author = "Helen Kane and Bob Saget"
SWEP.Purpose="Instant Self heal"
SWEP.Base="weapon_base"
SWEP.Spawnable=true
SWEP.UseHands=true
SWEP.ViewModel="models/props_junk/watermelon01.mdl"
SWEP.WorldModel="models/props_junk/watermelon01.mdl"
SWEP.ViewModelFOV=54
SWEP.Primary.ClipSize=1 
SWEP.Primary.DefaultClip=1
SWEP.Primary.Automatic=false
SWEP.Primary.Ammo="none"
SWEP.PrintName="Melon"
SWEP.Slot=6
SWEP.SlotPos=1
SWEP.HealAmount=50
local HealSound=Sound("ttt/nomnomnom.mp3")
local DenySound=Sound("whaawp.mp3")
function SWEP:Start()
	self:SetWeaponHoldType("grenade")
	timer.Create("melon_ammo" .. self:EntIndex(),1,0,function()
		if(!IsValid(self.Owner))then return end
		if(self:Clip1()<2)then self:TakePrimaryAmmo(-1)end
	end)
end
function SWEP:PrimaryAttack()
	if(!SERVER) then return end
		local ent = self.Owner
		if (IsValid(ent)&&self:Clip1()>=self.HealAmount&&ent:Health()<ent:GetMaxHealth())then
			self:TakePrimaryAmmo(self.HealAmount)
			ent:SetHealth(math.min(ent:GetMaxHealth(),ent:Health()+self.HealAmount))
			ent:EmitSound(HealSound)
			self:SendWeaponAnim(ACT_VM_PRIMARYATTACK)
			self:SetNextSecondaryFire(CurTime()+self:SequenceDuration()+1)
			self.Owner:SetAnimation(PLAYER_ATTACK1)
			timer.Simple(self:SequenceDuration(),function()if(!IsValid(self))then return end self:SendWeaponAnim(ACT_VM_IDLE)end)
		else
			ent:EmitSound(DenySound)
			self:SetNextSecondaryFire(CurTime()+1)
		end
end
function SWEP:OnRemove()
	timer.Stop("melon_ammo" .. self:EntIndex())
end
function SWEP:CustomAmmoDisplay()
	self.AmmoDisplay=self.AmmoDisplay or {}
	self.AmmoDisplay.Draw=true
	self.AmmoDisplay.PrimaryClip=self:Clip1()
	return self.AmmoDisplay
end


[ERROR] lua/oranges.lua:2: attempt to index global ‘SWEP’ (a nil value)

  1. unknown - lua/oranges.lua:2
    how do I format this text, jk got it.

(User was banned for this post ("wrong section" - postal))

If you have a code error, you should post both the code and the console error message so people can actually help you out. Otherwise we have no idea what went wrong in the code.

Nil value = What u try to define not exists. You made a,IF server. Then you do that you also need IF client

Ok, I’ll try that, is there anything else wrong with the code? Me and a friend did it, I don’t mean like errors, is there holes in the logic or such. Any advice I think is my question. :slight_smile: