Your inner loop is overwriting previously stored values in gameArray[i][j].y. This is because of this line:
gameArray[i][j] = tileArray[math.random(numTiles)]
Here, a random number, presumably from 1 to 25, is used to select a tile. But, it is likely that the same tile will be selected more that once, leading to the first value written to gameArray[i][1].y being overwritten when j is larger. I don't know exactly what functionality you desire here, but one solution is to build an array of indices, and to randomly select an index when selecting a tile, removing the index from the list so that it can not be selected again.
Here is an example of how this might be implemented. I have included some dummy initializations so that the code runs and displays results:
-- Dummy initializations
tileArray = {}
for i = 1, 25 do
tileArray[i] = {}
end
gameArray = {}
gridSize = {}
-- Initialize gridSize
gridSize.x = 5
gridSize.y = 5
-- List of available tile indices
indices = {}
for i = 1, 25 do
indices[i] = i
end
-- Create tilemap 5x5 array
for i = 1, gridSize.x do
gameArray[i] = {}
for j = 1, gridSize.y do
k = math.random(#indices)
index = indices[k]
table.remove(indices, k)
gameArray[i][j] = tileArray[index]
gameArray[i][j].x = (i * 100)
gameArray[i][j].y = (j * 100)
end
end
-- Display results
for i = 1, gridSize.x do
for j = 1, gridSize.y do
fmt = string.format("[%d][%d].x = %d, [%d][%d].y = %d\n",
i, j, gameArray[i][j].x, i, j, gameArray[i][j].y)
io.write(fmt)
end
end
Program output:
[1][1].x = 100, [1][1].y = 100
[1][2].x = 100, [1][2].y = 200
[1][3].x = 100, [1][3].y = 300
[1][4].x = 100, [1][4].y = 400
[1][5].x = 100, [1][5].y = 500
[2][1].x = 200, [2][1].y = 100
[2][2].x = 200, [2][2].y = 200
[2][3].x = 200, [2][3].y = 300
[2][4].x = 200, [2][4].y = 400
[2][5].x = 200, [2][5].y = 500
[3][1].x = 300, [3][1].y = 100
[3][2].x = 300, [3][2].y = 200
[3][3].x = 300, [3][3].y = 300
[3][4].x = 300, [3][4].y = 400
[3][5].x = 300, [3][5].y = 500
[4][1].x = 400, [4][1].y = 100
[4][2].x = 400, [4][2].y = 200
[4][3].x = 400, [4][3].y = 300
[4][4].x = 400, [4][4].y = 400
[4][5].x = 400, [4][5].y = 500
[5][1].x = 500, [5][1].y = 100
[5][2].x = 500, [5][2].y = 200
[5][3].x = 500, [5][3].y = 300
[5][4].x = 500, [5][4].y = 400
[5][5].x = 500, [5][5].y = 500
UPDATE
Given some more information about the goals of the OP code, it became apparent that you need to be able to use tiles from tileArray more than once. The problem is that by assigning an element from tileArray to gameArray[i][j], and then modifying gameArray[i][j], you are also modifying the original tile element.
The solution is to assign a copy of the tile element to gameArray[i][j]. Lua does not come with a function to copy tables, but you can look at this link to read about ways to copy tables. For a simple table requiring only a shallow copy, the page linked provides a function called shallowcopy(). Here is a modification of the above code that uses shallowcopy():
function shallowcopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in pairs(orig) do
copy[orig_key] = orig_value
end
else -- number, string, boolean, etc
copy = orig
end
return copy
end
-- Dummy initializations
numTiles = 5
tileArray = {}
for i = 1, numTiles do
tileArray[i] = {}
tileArray[i].tile_type = i
end
gameArray = {}
gridSize = {}
-- Initialize gridSize
gridSize.x = 5
gridSize.y = 5
-- Create tilemap 5x5 array
for i = 1, gridSize.x do
gameArray[i] = {}
for j = 1, gridSize.y do
gameArray[i][j] = shallowcopy(tileArray[math.random(numTiles)])
gameArray[i][j].x = (i * 100)
gameArray[i][j].y = (j * 100)
end
end
-- Display results
for i = 1, gridSize.x do
for j = 1, gridSize.y do
fmt = string.format("[%d][%d].x = %d, [%d][%d].y = %d : type %d\n",
i, j, gameArray[i][j].x, i, j, gameArray[i][j].y,
gameArray[i][j].tile_type)
io.write(fmt)
end
end
Program output:
[1][1].x = 100, [1][1].y = 100 : type 1
[1][2].x = 100, [1][2].y = 200 : type 1
[1][3].x = 100, [1][3].y = 300 : type 5
[1][4].x = 100, [1][4].y = 400 : type 2
[1][5].x = 100, [1][5].y = 500 : type 3
[2][1].x = 200, [2][1].y = 100 : type 5
[2][2].x = 200, [2][2].y = 200 : type 4
[2][3].x = 200, [2][3].y = 300 : type 2
[2][4].x = 200, [2][4].y = 400 : type 4
[2][5].x = 200, [2][5].y = 500 : type 3
[3][1].x = 300, [3][1].y = 100 : type 3
[3][2].x = 300, [3][2].y = 200 : type 5
[3][3].x = 300, [3][3].y = 300 : type 2
[3][4].x = 300, [3][4].y = 400 : type 4
[3][5].x = 300, [3][5].y = 500 : type 3
[4][1].x = 400, [4][1].y = 100 : type 4
[4][2].x = 400, [4][2].y = 200 : type 3
[4][3].x = 400, [4][3].y = 300 : type 5
[4][4].x = 400, [4][4].y = 400 : type 2
[4][5].x = 400, [4][5].y = 500 : type 2
[5][1].x = 500, [5][1].y = 100 : type 5
[5][2].x = 500, [5][2].y = 200 : type 5
[5][3].x = 500, [5][3].y = 300 : type 1
[5][4].x = 500, [5][4].y = 400 : type 5
[5][5].x = 500, [5][5].y = 500 : type 3