How to make a deck

So I’m working on hopefully a small test project and I wanna use cards in a way similar to Hearthstone or Bloxcards(?) or in my case specifically Wizard 101 only issue is I’m not sure how to kind of do it. I have this module script I’m not sure if it’s helpful, but I plan to basically put all the cards in the spells table.

local spells = {
	
	Choke = {
		Name = "Choke";
		School = "Fire";
		Level = "Any";
		Accuracy = 80;
		Pips = 2;
		Effect = "Stuns all enemies for 1 Round"
	};
	Immolate = {
		Name = "Immolate";
		School = "Fire";
		Level = "Any";
		Accuracy = 100;
		Pips = 4;
		Effect = "600 fire damage to target and 250 damage to self"
	};
	Scald = {
		Name = "Scald";
		School = "Fire";
		Accuracy = 75;
		Pips = 5;
		Effect = "465 fire damage to all enemies over 3 rounds"
	};
	
}
local school = {"Fire","Ice","Storm","Balance","Life","Death","Myth","Sun","Star","Moon","Shadow"}
setmetatable(spells,{
	__index = function(table,index,value)
		print(index.." doesn't exist!")
		print("Creating it")
		local randomSchool = math.random(1,#school);
		local selectedSchool = school[randomSchool]
		return {
			Name = index;
			School = tostring(selectedSchool);
			Level = math.random(0,100);
			Accuracy = math.random(0,100);
			Pips = math.random(1,20);
			print(index.." was created")
		}
	end
})
return spells

Now I have a few questions so first of all idk if the metatable is necessary I was just kind of using it because I just learned how it works, but how can I add what I just created into the spells table?

For my questions I’m trying to achieve 2 things.

  1. How can I randomly make a deck with a random selection of cards from the spells table? And make sure that no more than 3 of the same things get selected.
  2. How can I making a draw system? I tried doing this
local deck = {}
local totalAmountInDeck = #deck
local currentAmountInDeck = #deck

print("Cards")
print(currentAmountInDeck.." of "..totalAmountInDeck)
cardsInHand = 0
maxAmountOfCards = 7
while cardsInHand <= maxAmountOfCards do
	if cardsInHand == maxAmountOfCards then
		break
	end
	local draw = math.random(1,#deck)
	local drawn = deck[draw]
	table.remove(deck,draw)
	print(tostring(drawn))
	cardsInHand = cardsInHand + 1
end
print(currentAmountInDeck.." of "..totalAmountInDeck)

it doesn’t really work lol or it does but can probably make it better.

First of all, I don’t see a problem with your draw system. Second, to make your deck, you can make something very similar to your draw system and just repeat it until the deck is full. To make sure you don’t get more than three of the same card, when you draw a card you can loop through your current deck to make sure that card isn’t already there three times. If you detect that there are two of that card, you can add it and then remove the card from the pool so you don’t get it again. I recommend using a temporary copy of the card pool for this so you don’t remove the card for other shuffles.

Hmm this is what I’ve came up with so far now :confused: I feel like I’m on the right track but just a bit stuck still

	local allCards = {} --all the cards in the game?
	for i,v in pairs(cards) do
		table.insert(allCards,i)
	end
	
	local deck = {}
	local deckSize = 30
	
	for i = 1,deckSize do
		local random = math.random(1,#allCards)
		local chosen = allCards[random]
		local amountToPut = math.random(1,3)
		if not table.find(deck,chosen) then
			for b = 1,amountToPut do
				table.insert(deck,chosen)						
			end			
		end
	end

	if #deck == deckSize then
		print("deck filled")
		for i,v in pairs(deck) do
			print(v)
		end
	elseif #deck < deckSize then
		print("deck is missing cards "..#deck)
	elseif #deck > deckSize then
		print("deck has to many cards "..#deck)
	end

In that first for loop, did you mean to insert the index? It looks like you might have been trying to insert the card but you put the index instead.