Generating a card deck

I wrote a function to create a deck array however when I print it using

for i = 1, #deckArray do print(deckArray[i].rank, deckArray[i].suit)
end

My output is not correct returning

1 nil
2 nil
3 nil
4 nil
5 nil
6 nil
7 nil
8 nil
9 nil
10 nil
J nil
Q nil
nil clubs
1 clubs
2 clubs
3 clubs
4 clubs
5 clubs
6 clubs
7 clubs
8 clubs
9 clubs
10 clubs
J clubs
Q clubs
nil diamonds
1 diamonds
2 diamonds
3 diamonds
4 diamonds
5 diamonds
6 diamonds
7 diamonds
8 diamonds
9 diamonds
10 diamonds
J diamonds
Q diamonds
nil hearts
1 hearts
2 hearts
3 hearts
4 hearts
5 hearts
6 hearts
7 hearts
8 hearts
9 hearts
10 hearts
J hearts
Q hearts
nil spades
1 spades
2 spades
3 spades
4 spades
5 spades
6 spades
7 spades
8 spades
9 spades
10 spades
J spades
Q spades
K spades
A spades

I am trying to figure out where I went wrong in my createDeck function shown below.

local suitArray = {‘clubs’,‘diamonds’,‘hearts’,‘spades’}
local rankArray = {1,2,3,4,5,6,7,8,9,10,‘J’,‘Q’,‘K’,‘A’}
–create deck function
module.createDeck = function(deckArray)
for s=0,#suitArray do
for r=0,#rankArray do
deckArray[s*13+r]={
rank = rankArray[r],
suit = suitArray[s]
}
end
end

1 Like

tables in lua start at 1, your for loops start at 0, change the starting point on your loops to 1.

tried that and received this error

ServerScriptService.Script:20: attempt to index nil with ‘rank’

when trying to print using

for i = 1, #deckArray do print(deckArray[i].rank, deckArray[i].suit)