#Need help creating a shuffling system for a Stack of cards.
101 messages · Page 1 of 1 (latest)
Yes
ModifiersMS.Card_Range is 48 btw
you can initialize NewStack to have all numbers in order then shuffle it
Well the Stack has the numbers in order. Is that what you're saying?
for example you can start with {1, 2, 3, 4} using a for loop then shuffle it to {4, 2, 1, 3}
I don't really get what you're saying
This is what's printed when I print Stack. There's a number associated with the card, which is a table that stores information such as the Model its assigned to, whether or not its Face Up, its Owner, and its value.
local stack = {}
local function shuffle(tab)
for i = 1, #stack do
local j = math.random(1, #tab)
stack[i], stack[j] = stack[j], stack[i]
end
end
for i = 1, 48 do
stack[i] = i
end
shuffle(stack)
print(stack)
Eh?
the shuffle function will shuffle a given array
I was thinking of just replacing the numbers created in NewStack for the Index of the Stack argument passed.
But, okay, I'll try your method.
that code will return this
Doesn't work
** You are now Level 3! **
error ?
what did you print
The stack
can you show your code now
that's supposed to be before the other for loop
huh?
else this will shuffle an empty stack
Isn't the way you're doing this currently just.. not a good way of shuffling it, since the math.random function could pick the same number twice?
I'm just trying to randomly move the Card model positions in the stack
its like
I prefer doing it the way I was doing it beforehand, just tweaking a few things to actually make it work.
well what was the problem with the other way
the math.random in my version picks random indices, not values
I needed to take the Numbers generated in the NewStack table, and replace them with the Index of the Cards in the Stack.
so you want cards inside newstack, not numbers
The values from the first screenshot, should be replaced with the Index of the second screenshot.
That's what I want anyway
Well, I guess so
the index is that part
yeah that works
Really?
I don't know what would Card[Index] be though
Oh, I meant Stack[Index]
that will make a copy of Stack into NewStack
Well if I want to replace the NewStack's Value with the Stack's Index, would I need to do
NewStack[Card] = Stack[Index]```
that will error as Card is a table
Oh right.
do you even need "NewStack" or can you just shuffle Stack directly
I could probably shuffle Stack directly
I thought it would be better to do it this way
seems like unecessary memory usage
How would I rewrite the code to only use the Stack table though
this but tab is Stack
that algorithm is called Fisher–Yates shuffle if you want more info on it
and it won't have any duplicates as it only switches the indices
i got a bit confused with tab and stack, it should all be the same name as the parameter
Ok
Well it works, unfortunately it picks out random card models from the Stack instead of the top card descending.
what do you mean top card descending
Well I mean
** You are now Level 4! **
yknow the top card on the stack
yeah
yes
then the one under that
but now its taking out random cards from the middle
and it looks like its floating
which is wacky
im not sure how the shuffle could have done that
function StackManagement.Distribute() -- Distributes the Cards to all players in the game.
for _, Player in pairs(PlayersService:GetPlayers()) do -- Get all the players in the game.
local CardAmount = 1 -- Set the card amount as an integer value.
for Index = #StackManagement.Stack, 0, -1 do -- Goes through the Stack of cards in reversing order.
local Card = StackManagement.Stack[Index]-- Defines the Card.
if CardAmount <= 4 then -- This will run 4 times, moving 4 Cards to the player's Roster.
if not Card.Owner then -- Checks if the Card doesn't have an Owner.
Card.Owner = Player.Name -- Sets the Card.Owner property to the Player's username.
local CardLocation = RosterFolder[Card.Owner][CardAmount] -- Defines the CardLocation variable.
Card.Model:PivotTo(CardLocation:GetPivot()) -- Pivots the Card model to the CardLocation specified.
Card.Model.Parent = RosterFolder[Player.Name].Cards -- Moves the Card model's parent to the Player's Roster.
Card.Model.Main.Front.Number.Text = Card.Value -- Sets the Text on the Card to equal the Card's Value.
FaceUpRM:FireClient(Player, Card, CardAmount) -- Fires to the Client to set the FaceUp properties of the first 2 Cards to true.
CardAmount += 1 -- Adds 1 to the Card Amount.
end
end
end
end
end```
This is the distribute script, which is what's actually taking out the cards.
so you shuffle the stack beforehand
Yeah
I create the Stack
shuffle it
then distribute it
function StackManagement.StackCards(Stack) -- Stack the Cards in the Workspace.
-- The Cards argument passed is the Stack table, which stores all 48 cards.
local NextStackLocation = nil -- Defines the NextStackLocation, which is the previous Card's position + 0.02.
for Index, Card in pairs(Stack) do -- Loop through the Cards.
Card.Model:PivotTo(NextStackLocation or StackFolder.StackLocation.CFrame + Vector3.new(0, 0.02, 0)) -- Moves the CardModel to either the NextStackLocation, or the StackLocation.CFrame, which is the point where the Stack begins.
NextStackLocation = Card.Model:GetPivot() + Vector3.new(0, 0.02, 0) -- Defines the NextStackLocation property as the Previous Card.
Card.Model.Name = "Card_" .. Index -- Sets the name of the Card Model in the Workspace to the Index.
wait()
end
StackManagement.Shuffle(Stack)
end
This is what's calling the Shuffle function
you should be shuffling it before displaying the cards
as they will not move after you shuffled the stack
so
- Shuffle(Stack)
- StackCards(Stack)
- Distribute(Stack)
Ok
Minor issue
The cards is being created in this function
function StackManagement.NewCard() -- Creates a new card with the assigned properties.
for Index = 1, ModifiersMS.Value_Range do -- Creates 48 cards in total.
for _ = 1, (ModifiersMS.Card_Range / ModifiersMS.Value_Range) do -- Assigns the values for the cards. 4 Cards are assigned to 1 value.
local Card = { -- Stores the card information, including the Model, Value, Owner, and if the card is Face Up.
Model = script.Card:Clone(),
Owner = nil,
Value = Index,
FaceUp = false,
}
Card.Model.Parent = StackFolder -- Sets the Model of the Card to the StackFolder in the Workspace.
table.insert(StackManagement.Stack, Card) -- Inserts the Card into the Stack table.
end
end
StackManagement.StackCards(StackManagement.Stack) -- Calls the StackCards function.
end
which then calls the StackCards function. If I call Shuffle first, it won't have anything inside of the Stack table
you can shuffle the stack here
Ok