I am trying to create a weapon for a friend on Garry's Mod with Lua. I have made it through all other errors but I keep getting this error:
input:22: attempt to index global SWEP (a nil value)
I try to put a function before line 22 where it would say
function SWEP.Base = "weapon_tttbase"
but then I would get an error saying '(' expected near =
if SERVER then
AddCSLuaFile( "shared.lua" )
SWEP.HoldType = "normal"
end
if CLIENT then
SWEP.PrintName = "Cloak"
SWEP.Slot = 6
SWEP.ViewModelFlip = false
SWEP.EquipMenuData = {
type = "item_weapon",
desc = "cloak_desc"
};
SWEP.Icon = "VGUI/ttt/icon_disguiser"
end
SWEP.Base = "weapon_tttbase"
SWEP.UseHands = true
SWEP.ViewModelFlip = false
SWEP.ViewModelFOV = 70
SWEP.ViewModel = "models/weapons/v_models/v_watch_spy.mdl"
SWEP.WorldModel = "models/weapons/w_models/w_watch.mdl"
SWEP.ShowViewModel = true
SWEP.ShowWorldModel = true
SWEP.ViewModelBoneMods = {}
SWEP.Spawnable = true
SWEP.AdminSpawnable = true
SWEP.DrawCrosshair = false
SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = false
SWEP.Primary.Delay = 129.0
SWEP.Primary.Ammo = "none"
SWEP.Primary.ClipMax = -1
SWEP.AutoSpawnable = false
SWEP.AmmoEnt = "none"
SWEP.Kind = WEAPON_EQUIP1
SWEP.CanBuy = {ROLE_DETECTIVE} -- only detectives can buy
SWEP.LimitedStock = true -- only buyable once
SWEP.Primary.Sound = Sound( "hl2_sound_misc_000" )
-- Pull out faster than standard guns
SWEP.DeploySpeed = 2
SWEP.AllowDrop = false
SWEP.NoSights = true
SWEP.Initialize()
self.Weapon:SetNetworkFloat("energy",100)
function SWEP:Think()
if SERVER then
if( self.Owner:KeyDown( IN_ATTACK ) ) then
if self.Weapon:GetNetworkedFloat("energy") >0 then
if self.Weapon:GetNetworkedFloat("energy") >0 then
end
end
end
self.Weapon:SetNetworkedFloat("energy",self.Weapon:GetNetworkedFloat("energy")-0.1)
end
if (self.Invis) then if self.Invis:IsValid() then return end
end
self.Invis = ents.Create("ms_cloakent")
self.Invis:SetPos(
self.Owner:GetPos()+Vector(0,0,50) )
self.Invis:SetAngles( Vector(0,0,0) )
self.Invis:SetParent( self.Owner )
self.Invis:SetNetworkedEntity("parent",self.Owner)
self.Invis:SetNetworkedBool("set",true)
self.Invis:Spawn()
self.Owner:DrawWorldModel(false)
self.Owner:SetColor(255,255,255,0)
end
if (self.Invis) then
if self.Invis:IsValid() then
self.Invis:Remove()
end
end
self.Owner:DrawWorldModel(true)
self.Owner:SetColor(255,255,255,255)
if (self.Invis) then
if self.Invis:IsValid() then
self.Invis:Remove()
end
end
self.Owner:DrawWorldModel(true)
self.Owner:SetColor(255,255,255,255)
if self.Weapon:GetNetworkedFloat("energy") <100 then
self.Weapon:SetNetworkedFloat("energy",self.Weapon:GetNetworkedFloat("energy"))
end
function SWEP:PrimaryAttack()
local effect = EffectData()
effect:SetOrigin(self.Owner:GetPos())
effect:SetScale(100)
util.Effect("super_explosion2", effect)
local effect = EffectData()
effect:SetOrigin(self.Owner:GetPos())
effect:SetScale(50)
util.Effect("super_explosion2", effect)
end
function SWEP:OnRemove()
if (self.Invis) then
if self.Invis:IsValid() then
self.Invis:remove()
end
end
return true
end
function SWEP (Holster)
if (self.Invis) then
if self.Invis:IsValid() then
self.Invis:Remove()
end
end
return true
end
if CLIENT then
function SWEP (DrawHUD)
draw.RoundedBox( 10, ScrW()/2-20, ScrH/2-20, 40, 40,
Color(0,0,0,100) )
draw.SimpleText(
tostring(math.Round(self.Weapon:GetNetworkedFloat("energy"))).."%",
"ChatFont", ScrW()/2, ScrH/2, Color(255,255,255,255), TEXT_ALIGN_CENTER,
TEXT_ALIGN_CENTER )
end
function SWEP (OnDrop)
self:Remove()
end
function SWEP:PrimaryAttack()
self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
self:Cloak()
end
end
Unless SWEP is a table that already exists when your code is loaded you are attempting to assign to SWEP.Base but SWEP does not exist (it is nil as the error indicates).
Also, you have multiple definitions of a SWEP function later. Only the last of which will actually exist when your code is finished running. That definition will also replace whatever table you had originally filled with all of your various SWEP.* assignments.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.