2

我不断弹出此代码,但我不确定错误是什么。有人可以帮我弄清楚吗 - 它说它在第 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
4

1 回答 1

2

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

假设“第 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错误。

于 2012-11-03T05:45:56.267 回答