Help With Creating Randomized Card Deck

Hello! I am trying to create a randomized card deck for my game, but have ran into the following problems:
1. I can’t use the same key in a table twice
2. when shuffling cards, the deck doesn’t get shuffled efficiently, and a lot of the same cards are next to each other

For my second problem I thought of using a filter list that would add a card to the list if it was just added to the deck so none of those types of cards would be added right after, and would be removed after the next card was added, but that would lead to my first problem of not being able to use the same key.

items = {
	nuke = {
		name = "Nuke";
		imageId = nil;
		amount = 6
	};
	defuser = {
		name = "Defuser";
		imageId = nil;
		amount = 6
	};
	attack = {
		name = "Attack";
		imageId = nil;
		amount = 4
	};
	steal = {
		name = "Steal";
		imageId = nil;
		amount = 4
	};
	stop = {
		name = "Stop";
		imageId = nil;
		amount = 5
	};
	shuffle_blocks = {
		name = "Shuffle Blocks";
		imageId = nil;
		amount = 4
	};
	skip = {
		name = "Skip";
		imageId = nil;
		amount = 4
	};
	see_the_future = {
		name = "See the Furture";
		imageId = nil;
		amount = 5
	};
}

I’d figured I would create a table of all the cards in my game and add properties to them. The amount represents the amount of those cards I’d like to add to the deck, but I am having trouble doing that since you can’t use the same key multiple times. Any help is appreciated! :slight_smile:

1 Like

What do you mean by “you can’t use the same key multiple times”?

1 Like

I think when I said table I meant dictionary, but I’m pretty sure if you try to create a dictionary with the same key multiple times it doesn’t work.
For example:

local idkTable = {
    ["hi"] = "hello";
    ["hi"] = "bye"
}
print(idkTable["hi"]) -- this would randomly print one of the two

but in my case if I try adding a value with the same key multiple times in a dictionary, it would error

Yeah, the key below overrides the key above. Why do you need to add a value with the same key multiple times?

I want to add a certain card multiple times into the deck table multiple times, but when I tried it wasn’t working. I know I could use numbers after each card like attack_1, attack_2, and attack_3, but I’m not sure if there is a better way of doing this.

You could use an array instead of a dictionary.

1 Like

Store it as an array then where the index would correspond to the position of the card in deck and the value will be a table with a name and imageID fields.

1 Like

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