user1795799
user1795799

Reputation: 21

LUA Error Code 64: Attempt to index field '?' (a nil value)

I keep getting this code popping up but I am not sure what the error is. Can someone please help me figure it out - it says it is on line 64. Sorry I wasn't sure how to put it in code. Here is the error line code

        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

Upvotes: 2

Views: 9243

Answers (1)

Hisham H M
Hisham H M

Reputation: 6828

That error means that you are trying to index a nil value.

Assuming that "line 64" is the first one from the code you posted:

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

This means that array[j] is nil: in other words, that there is no value with index j in your array. You may want to check it like this:

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

Note that you have to test both array[j] and array[j][1], because if array[j] exists but array[j][1] doesn't, the < comparison will cause an attempt to compare nil with number error.

Upvotes: 2

Related Questions