LUA错误代码64:尝试索引字段'?' (零值)

时间:2012-11-03 01:46:22

标签: lua

我一直在弹出这段代码,但我不确定错误是什么。有人可以帮我解决一下 - 它说它在第64行。抱歉我不知道如何把它放在代码中。这是错误行代码

        if ( array[j][1] < smallest ) then

            smallest = array[j][1]
            index = j   
        end




--------- [ Element Data returns ] ---------
local function getData( theElement, key )
    local key = tostring(key)
    if isElement(theElement) and (key) then

        return exports['[ars]anticheat-system']:c_callData( theElement, tostring(key) )
    else
        return false
    end
end 

-------------- [ Scoreboard ] ---------------
local screenX, screenY = guiGetScreenSize()

local width, height = 300, 420
local x = (screenX/2) - (width/2)
local y = (screenY/2) - (height/2)

local logowidth, logoheight = 275, 275
local logox = (x/2) - (logowidth/2)
local logoy = (y/2) - (logoheight/2)

local isEventHandled = false
local page = 1

local a = { "Z", "W", "Y", "G", "H", "L", "P", "A", "B" }
function getPlayerIDTable( )

    local array = { }

    for key, thePlayer in ipairs ( getElementsByType("player") ) do

        local playerID = tonumber( getData( thePlayer, "playerid") )
        if ( playerID ) then

            array[#array + 1] = { playerID, thePlayer }
        end
    end

    for i = 1, 9 do

        local j = math.random( 1, 9 )
        if ( array[i] == nil ) then

            array[i] = { j, a[ math.random( 1, 9 ) ] }
        end 
    end

    return array
end

local players = { }
function assemblePlayersByID( )

    local array = getPlayerIDTable( )
    local smallest, index, tempo

    for i = 1, #array do

        smallest = array[i][1]
        index = i

        for j = i + 1, #array do
            if ( array[j][1] < smallest ) then

                smallest = array[j][1]
                index = j   
            end
        end

        -- flip arrays
        tempo = array[i]
        array[i] = array[index]
        array[index] = tempo
    end

1 个答案:

答案 0 :(得分:2)

该错误意味着您正在尝试索引零值。

假设“第64行”是您发布的代码中的第一行:

if ( array[j][1] < smallest ) then

这意味着array[j]为nil:换句话说,数组中没有索引j的值。您可能需要像这样检查:

if array[j] and array[j][1] and array[j][1] < smallest then

请注意,您必须同时测试array [j]和array [j] [1],因为如果array [j]存在但array [j] [1]没有,则<比较将导致attempt to compare nil with number错误。