尝试索引本地'v'(零值) [英] Attempt to index local 'v' (a nil value)

查看:177
本文介绍了尝试索引本地'v'(零值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编辑一个lua脚本以添加到我的Garry的Mod服务器上,但我收到此错误,我不知道该怎么做。

I'm trying to edit a lua script to add on my Garry's Mod Server, but I get this error and I dont know what to do.

错误:


[错误] - / sh_worlditemspawner.lua:56:尝试索引本地'v'(零值)

[ERROR] --/sh_worlditemspawner.lua:56: attempt to index local 'v' (a nil value)

代码:

local PLUGIN = PLUGIN
PLUGIN.name = "World Item Spawner"
PLUGIN.author = "Black Tea"
PLUGIN.desc = "World Item Spawner."
PLUGIN.itempoints = PLUGIN.itempoints or {}
PLUGIN.spawngroups = { 
    ["default"] = {
        {"bleach"},
    },
    ["example"] = {
        {"ration"},
    },
    ["junks"] = { 
        {"junk_ws"},
        {"junk_wj"},
        {"junk_be"},
        {"junk_bt"},
        {"junk_p"},
        {"junk_ss"},
        {"junk_bl"},
        {"junk_k"},
        {"junk_p"},
        {"junk_hp"},
        {"junk_ec"},
        {"junk_ej"},
    }
}

PLUGIN.spawnrate = 30
PLUGIN.maxitems = 10
PLUGIN.itemsperspawn = 2
PLUGIN.spawneditems = PLUGIN.spawneditems or {}

if SERVER then
    local spawntime = 1
    function PLUGIN:Think()
        if spawntime > CurTime() then return end
        spawntime = CurTime() + self.spawnrate
        for k, v in ipairs(self.spawneditems) do
            if (!v:IsValid()) then
                table.remove(self.spawneditems, k)
            end
        end

        if #self.spawneditems >= self.maxitems then return end

        for i = 1, self.itemsperspawn do
            if #self.spawneditems >= self.maxitems then return end
            local v = table.Random(self.itempoints)
            if #self.spawneditems > self.maxitems then
                return
            end


            local data = {}
            data.start = v[1]
            data.endpos = data.start + Vector(0, 0, 1)
            data.filter = client
            data.mins = Vector(-16, -16, 0)
            data.maxs = Vector(16, 16, 16)
            local trace = util.TraceHull(data)

            if trace.Entity:IsValid() then
                continue
            end

            local idat = table.Random(self.spawngroups[v[2]]) or self.spawngroup["default"]
            local item = nut.item.Spawn(v[1] + Vector( math.Rand(-8,8), math.Rand(-8,8), 10 ), AngleRand(), idat[1], idat[2] or {})
            table.insert( self.spawneditems, item )
        end
    end

    function PLUGIN:LoadData()
        self.itempoints = nut.util.ReadTable("itempoints")
    end

    function PLUGIN:SaveData()
        for k, v in ipairs(self.spawneditems) do
            v:Remove()
        end
        nut.util.WriteTable("itempoints", self.itempoints)
    end
else
    netstream.Hook("nut_DisplaySpawnPoints", function(data)
        for k, v in pairs(data) do
            local emitter = ParticleEmitter( v[1] )
            local smoke = emitter:Add( "sprites/glow04_noz", v[1] )
            smoke:SetVelocity( Vector( 0, 0, 1 ) )
            smoke:SetDieTime(10)
            smoke:SetStartAlpha(255)
            smoke:SetEndAlpha(255)
            smoke:SetStartSize(64)
            smoke:SetEndSize(64)
            smoke:SetColor(255,186,50)
            smoke:SetAirResistance(300)
            emitter:Finish()
        end
    end)
end

nut.command.Register({
    adminOnly = true,
    onRun = function(client, arguments)
        local trace = client:GetEyeTraceNoCursor()
        local hitpos = trace.HitPos + trace.HitNormal*5
        local spawngroup = arguments[1] or "default"
        table.insert( PLUGIN.itempoints, { hitpos, spawngroup } )
        nut.util.Notify( "You added ".. spawngroup .. " item spawner." )
    end
}, "itemspawnadd")

nut.command.Register({
    adminOnly = true,
    onRun = function(client, arguments)
        local trace = client:GetEyeTraceNoCursor()
        local hitpos = trace.HitPos + trace.HitNormal*5
        local range = arguments[1] or 128
        local mt = 0
        for k, v in pairs( PLUGIN.itempoints ) do
            local distance = v[1]:Distance( hitpos )
            if distance <= tonumber(range) then
                PLUGIN.itempoints[k] = nil
                mt = mt + 1
            end
        end
        nut.util.Notify( mt .. " item spawners has been removed.")
    end
}, "itemspawnremove")

nut.command.Register({
    adminOnly = true,
    onRun = function(client, arguments)
        if SERVER then
            netstream.Start(client, "nut_DisplaySpawnPoints", PLUGIN.itempoints)
            nut.util.Notify( "Displayed All Points for 10 secs." )
        end
    end
}, "itemspawndisplay")


推荐答案

这是因为

table.Random(self.itempoints)

返回nil。你的意思是math.random吗?如果您发布该表的代码。随机函数我可以提供更多信息。

returns nil. Did you mean math.random? If you post code for that table.Random func I can give more info.

这篇关于尝试索引本地'v'(零值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆