Correctly placing every card in a deck after deleting one

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:

Deck

1 Like

I have a similar system like you with my order UI and every order that got done it would reorginise the deck the way I did it is by getting the first card’s position and check if it’s in the same position in the frame if not there it would adjust to the first position which is UDim2.new(0,0,0,0).

I would calculate the size of the frame and then position them with the size and a gap between them but in ur case it be better to get their size and substracting a bit.

I used a tween to reposition all frames but you can do without the tween.

local function RepositionOrders()
	local LastPosition = UDim2.new(0,0,0,0)
	
	for i,orderUI in ipairs(OrdersFrame:GetChildren()) do
		if orderUI:GetAttribute("Done") == "Pending" then
			if orderUI.Position == LastPosition then
				LastPosition += UDim2.new(0,orderUI.Size.X.Offset + 15,0,0)
			elseif orderUI.Position ~= LastPosition then
				local SlideTween = TweenService:Create(orderUI, TweenInfo.new(1, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {Position = LastPosition})
				SlideTween:Play()
				
				LastPosition += UDim2.new(0,orderUI.Size.X.Offset + 15,0,0)
				
				task.wait(.1)
			end
		end
	end
end

Thanks, I would’ve never thought of something like that you probaly saved me days of trying to figure out how to it. There are still some small bugs but atleast it’s sorting everything correctly.

No problem, yea it’s best I got right now.

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