What's wrong with this?

--[[
Error:

Main:90: attempt to index a nil value (field '?')
stack traceback:
	Main:90: in function 'checkWins'
	Main:38: in function 'draw'

Code:

> Line 89 - 91:

-- Diagonally (right-top -> left-bottom)
elseif tab[x+3][y] == 1 and tab[x+2][y+1] == 1 and tab[x+1][y+2] == 1 and tab[x][y+3] == 1 then
msg = "Blue Wins!"

> Line 38

checkWins()

What's wrong?
]]

Check the values of X, most likely X+3 is getting too big and going outside the limits of your table

Check the values of X, most likely X+3 is getting too big and going outside the limits of your table

But then the error message should appear for other if-tags too:

tab[x][y] == 1 and tab[x+1][y+1] == 1 and tab[x+2][y+2] == 1 and tab[x+3][y+4] == 1
    
tab[x][y] == 1 and tab[x+1][y] == 1 and tab[x+2][y] == 1 and tab[x+3][y] == 1
    
tab[x][y] == 1 and tab[x][y+1] == 1 and tab[x][y+2] == 1 and tab[x][y+3] == 1

This three worked for every X and Y, and the table for the checkWins is as big as the drawen one.

Check your for loop size

It’s hard to advise without seeing the complete loop

Add some print statements to see what the values are.

With two dimensional arrays you can check whether each element is valid before querying it. Eg your code

elseif tab[x+3][y] == 1

Will produce an error if x+3 is out of range, but if you do this:

elseif tab[x+3] and tab[x+3][y] == 1

Lua first checks whether tab[x+3] is valid before trying to get a value from it. If tab[x+3] doesn’t exist it will return false and the second part of the expression won’t be evaluated, avoiding the error.

Of course, merely avoiding the error could produce other unexpected results, so it’s probably best to separate them out into explicit checks, maybe like this

if tab[x+3] and tab[x+3][y] then
   if tab[x+3][y] == 1 then
     --do something
   end
else
   -- handle query out of range error
end

Okay, thank you. And I already found another way… I have a Drawen and a setup-table. I just made the setup table bigger :slight_smile: