1

我正在尝试使用 Lua 在 Garry's Mod 上为朋友创建武器。我已经解决了所有其他错误,但我不断收到此错误:

input:22: attempt to index global SWEP (a nil value)

我尝试在第 22 行之前放置一个函数

function SWEP.Base = "weapon_tttbase"但后来我会得到一个错误说'(' 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
4

1 回答 1

3

除非 SWEP 是加载代码时已经存在的表,否则您将尝试分配给 SWEP.Base 但 SWEP 不存在(如错误所示,它为 nil)。

此外,您稍后还有多个 SWEP 函数定义。当您的代码完成运行时,只有最后一个会实际存在。该定义还将替换您最初填写的所有各种 SWEP.* 分配的任何表格。

于 2013-06-28T18:10:18.180 回答