Hello,
I’ve been trying to make a card deck and now I want to remove cards using the module script but I can’t figure out how. I would appreciate any help and If you need more details, code or anything else please ask I’d be happy to help.
Module Script:
-- @ Create
--// Variables
local deck = {}
local card = script:WaitForChild("Card")
local cardsGui = script.Parent
local cardsHolder = cardsGui:WaitForChild("Holder")
local cardSpawnPosition = UDim2.new((1 - card.Size.X.Scale) / 2, 0, 2, 0)
local cardSpacing = 0.0914
local tweenService = game:GetService("TweenService")
local tweenInformation = TweenInfo.new(0.5, Enum.EasingStyle.Quart)
--// Functions
function getAngle(origin: Frame, target: Frame)
local x, y = target.AbsolutePosition.X - origin.AbsolutePosition.X, target.AbsolutePosition.Y - origin.AbsolutePosition.Y
return math.deg(math.atan2(y, x))
end
function getCards()
local cards = {}
for _, child in pairs(cardsHolder:GetChildren()) do
if child.Name == "Card" then
cards[child.ZIndex] = child
end
end
return cards
end
function deck.newCard(cardType: Color3, cardName: string)
local newCard = card:Clone()
local placeholderCard = card:Clone()
local cardsInDeck = #getCards()
local cardValue = newCard:WaitForChild("Value")
local positionInDeck = UDim2.new(cardsInDeck * cardSpacing + card.Position.X.Scale, 0, card.Position.Y.Scale, 0)
local tweenGoals = {
Position = positionInDeck,
Rotation = 0
}
local cardTween = tweenService:Create(newCard, tweenInformation, tweenGoals)
placeholderCard.Name = "Placeholder Card"
placeholderCard.Parent = cardsHolder
placeholderCard.Position = positionInDeck
newCard.Parent = cardsHolder
newCard.Position = cardSpawnPosition
newCard.Rotation = getAngle(newCard, placeholderCard)
newCard.ZIndex = cardsInDeck + 1
cardValue.TextColor3 = cardType
cardValue.Text = cardName
cardValue.ZIndex = cardsInDeck + 2
placeholderCard:Destroy()
cardTween:Play()
end
function deck.removeCard(cardType: Color3, cardName: string)
for _, card in pairs(getCards()) do
local cardValue = card:WaitForChild("Value")
if cardValue.Text == cardName and cardValue.TextColor3 == cardType then
card:Destroy()
organizeDeck()
end
end
end
function organizeDeck()
local cards = getCards()
for index, cardInDeck in pairs(cards) do
if not cards[index - 1] then
cards[index] = nil
cards[index - 1] = cardInDeck
cardInDeck.ZIndex -= 1
cardInDeck:WaitForChild("Value").ZIndex -= 1
local positionInDeck = UDim2.new((index - 2) * cardSpacing + card.Position.X.Scale, 0, card.Position.Y.Scale, 0)
local tween = tweenService:Create(cardInDeck, tweenInformation, { Position = positionInDeck })
tween:Play()
end
end
end
return deck
Deck Preview: