luacoronasdk

attempt to index field ? (nil value)


I am not sure where the problem is. Anyone know why?

function check(board, color, row, col)
--if same color, change tile to "o"

if board[row][col] == color then -- attempt to index nil?
    board[row][col] = "o"
    count = count + 1
    return "o"
end

return

end


Solution

  • The problem is that board[row] is not defined; it's nil. So you are trying to do nil[col].

    You can avoid this error by doing this:

    if board[row] and board[row][col] == color then
    

    Instead.

    However, I'd recommend you to review the way board is created - for example, make sure that you have not switched rows and cols somewhere in your code by mistake.