Garry 的 mod lua 代码错误不起作用(实体 X,为零)

Garry's mod lua code error not working (Entity X, got nil)

我试图修复一些 Garry 的 Mod 插件,结果就是这样。我试图修复它很长时间,但我在 Lua 编码方面不是最好的:/。这段代码有什么问题?我收到此错误:

[ERROR] addons/garrys_bombs_5_base_528449144/lua/entities/gb5_shockwave_sound_lowsh.lua:80: bad argument #1 to 'SetPhysicsAttacker' (Entity expected, got nil)
  1. SetPhysicsAttacker - [C]:-1
   2. unknown - addons/garrys_bombs_5_base_528449144/lua/entities/gb5_shockwave_sound_lowsh.lua:80

而且代码很长。我的每个文件都工作正常,但这个文件不工作

    AddCSLuaFile()

DEFINE_BASECLASS( "base_anim" )

if (SERVER) then
    util.AddNetworkString( "gb5_net_sound_lowsh" )
end

ENT.Spawnable                        =  false
ENT.AdminSpawnable                   =  false     

ENT.PrintName                        =  ""        
ENT.Author                           =  ""      
ENT.Contact                          =  ""      

ENT.GBOWNER                          =  nil            
ENT.MAX_RANGE                        = 0
ENT.SHOCKWAVE_INCREMENT              = 0
ENT.DELAY                            = 0
ENT.SOUND                            = ""

net.Receive( "gb5_net_sound_lowsh", function( len, pl )
    local sound = net.ReadString()
    LocalPlayer():EmitSound(sound)

end );

function ENT:Initialize()
     if (SERVER) then
         self.FILTER                           = {}
         self:SetModel("models/props_junk/watermelon01_chunk02c.mdl")
         self:SetSolid( SOLID_NONE )
         self:SetMoveType( MOVETYPE_NONE )
         self:SetUseType( ONOFF_USE ) 
         self.Bursts = 0
         self.CURRENTRANGE = 0
         self.GBOWNER = self:GetVar("GBOWNER")
         self.SOUND = self:GetVar("SOUND")


     end
end

function ENT:Think()        
     if (SERVER) then
     if not self:IsValid() then return end
     local pos = self:GetPos()
     self.CURRENTRANGE = self.CURRENTRANGE+(self.SHOCKWAVE_INCREMENT*10)
     if(GetConVar("gb5_realistic_sound"):GetInt() >= 1) then
         for k, v in pairs(ents.FindInSphere(pos,self.CURRENTRANGE)) do
             if v:IsPlayer() then
                 if not (table.HasValue(self.FILTER,v)) then
                    net.Start("gb5_net_sound_lowsh")
                        net.WriteString(self.SOUND)
                    net.Send(v)
                    v:SetNWString("sound", self.SOUND)
                    if self:GetVar("Shocktime") == nil then
                        self.shocktime = 1
                    else
                        self.shocktime = self:GetVar("Shocktime")
                    end
                    if GetConVar("gb5_sound_shake"):GetInt()== 1 then
                        util.ScreenShake( v:GetPos(), 5555, 555, self.shocktime, 500 )
                    end
                    table.insert(self.FILTER, v)

                 end
             end
         end
     else
        if self:GetVar("Shocktime") == nil then
            self.shocktime = 1
        else
            self.shocktime = self:GetVar("Shocktime")
        end
        local ent = ents.Create("gb5_shockwave_sound_instant")
        ent:SetPos( pos ) 
        ent:Spawn()
        ent:Activate()
        ent:SetPhysicsAttacker(ply)
        ent:SetVar("GBOWNER", self.GBOWNER)
        ent:SetVar("MAX_RANGE",50000)
        ent:SetVar("DELAY",0.01)
        ent:SetVar("Shocktime",self.shocktime)
        ent:SetVar("SOUND", self:GetVar("SOUND"))
        self:Remove()
     end
     self.Bursts = self.Bursts + 1
     if (self.CURRENTRANGE >= self.MAX_RANGE) then
         self:Remove()
     end
     self:NextThink(CurTime() + (self.DELAY*10))
     return true
     end
end
function ENT:OnRemove()
    if SERVER then
        if self.FILTER==nil then return end
        for k, v in pairs(self.FILTER) do
            if not v:IsValid() then return end
            v:SetNWBool("waiting", true)
        end
    end
end
function ENT:Draw()
     return false
end

有人可以帮我解决这个问题吗?或者只是告诉我哪里出了问题?我会很高兴。如果需要,我可以发送所有文件。好吧...这不是我的插件,但我正在尝试修复现有的插件。有人也试图修复它,但他没有(实际上他把它弄坏了更多)。

错误是什么意思

  • 在您的 ENT:Think() 函数中,您正在调用 ent:SetPhysicsAttacker(ply)
  • ply 在该函数内部的任何地方都没有定义,因此是 nil (Entity expected, got nil)

如何解决这个问题

如果没有玩家对此实体造成的伤害负责,请删除行ent:SetPhysicsAttacker(ply)

否则,使用 SetOwner.
在创建时将 Owner 分配给实体 这将允许您在 Think 钩子

中使用 self:GetOwner()

例子

hook.Add("PlayerSay", "SpawnEntity", function(ply, text)

    if string.lower(text) == "!spawnentity" then
        -- Create your entity
        local myEntity = ents.Create("gb5_shockwave_sound_lowsh")
        myEntity:SetPos(ply:GetPos())
        myEntity:SetAngles(ply:GetAngles())
        myEntity:Spawn()

        -- Sets the owner to the player that typed the command
        myEntity:SetOwner(ply)

        return ""
    end

end)


-- Inside your entity code
function ENT:Think()
    print("My owner is: " .. tostring(self:GetOwner()))

    -- ...

    ent:SetPhysicsAttacker(self:GetOwner())
end