How to check for the amount of a specific thing within a table

I am looking to find the amount of a specific card within a table that I’ve made. This is because if the player only has 2 cards in their deck, I don’t want them to be able to equip more than that, so I want to check their equipped cards for how many of the same card they have and check if that number is the same or less than how many of that card they have in their deck.

#insertTableNameHere returns the total amount of things within a table, and table.find() just returns the first index of whatever you enter into it, and not the quantity of things within that table.

I’ve run into a problem here in my EquipCard function where I’m not sure how to check if the player has enough cards to equip more than one. In my third if check, I’m preventing the player from equipping more than one if they only have one of that card in their deck, but if they only have two of a card, nothing is stopping them from equipping four of that card.

Sorry for the long explanations as I wasn’t sure how to best describe my problem.

function DeckClient.EquipCard(cardName, cardHovered: ImageButton)
	
	local card: Types.Card = DeckClient.RetrieveCardInDeck(cardName)
	
	if card == nil then return end
	if #equippedCards >= 4 then return end
	if table.find(equippedCards, card) and card.CardAmount == 1 then return end
	
	table.insert(equippedCards, card)
	
	local equippedCardIcon = RS.CardsGui.EquippedCard:Clone()
	equippedCardIcon.Image = "http://www.roblox.com/asset/?id=" .. card.ImageId
	equippedCardIcon.Name = "EquippedCard" .. #equippedCards
	equippedCardIcon.Parent = equippedCardsList
	
end

Came up with a solution, here’s how my if checks look like now. I just made a new variable and increased it for every card with the same name found within the equipped cards table, and then if that number ever surpasses the amount of that card in the deck it will end the function there.

local amountEquippedOfCard = 1

if card == nil then return end
if #equippedCards >= 4 then return end
if table.find(equippedCards, card) and card.CardAmount == 1 then return end

for i, equippedCard in pairs(equippedCards) do
		if equippedCard.Name == card.Name then
			amountEquippedOfCard += 1
			if amountEquippedOfCard > card.CardAmount then return end
		end
	end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.