# How do I make a card pack?

How would I make a card pack? Like you use money and you buy the pack, then you get 5 random items.

2 Likes

Firstly, Iâ€™d like to note that if these will be purchasable through Robux (directly or indirectly), you must show the drop rates of the possible prizes.

To do this, you may just simply want it completely random and have a list of items, generate a random number using Random. Otherwise, if you want it weighted, youâ€™d have to have a dictionary with their weightings like so:

``````local items = {
["Item 1"] = 1; -- Very rare
["Item 2"] = 10;
["Item 3"] = 50;
["Item 4"] = 100; -- Common
}
``````

and then youâ€™d want to build an array based on their weightings and pick a random item from that list like this:

``````local newItems = {}
for name, weight in pairs(items) do
for i = 1, weight do
table.insert(newItems, name)
end
end

local randomNum = Random.new():NextInteger (1, #newItems)
local randomItem = newItems[randomNum]
local chance = items[randomItem]/#newItems

print(randomItem, chance)
``````

(this could probably get quite memory intensive if you have very common items or lots of items, so Iâ€™m not saying this is the best solution, but itâ€™s one that works)

3 Likes

Assuming you have a table with all of the items in it, you could get random from 1 to #your-table and then just return the value.

To read up on math.random just check out this

Also yes check out generalâ€™s code it is very informative

1 Like

Thatâ€™s not a link to math.random, though? `math.random` is part of the math library.

2 Likes

Plug in: repeating an item n amount of times in a new array based on a number is quick, dirty and inefficient. A proper weighted table typically creates a number out of all weightings and selects a random sum between 1 and this number. From here, a random item is continually selected and its weight is added to a value until that value is equal to or surpasses the randomised weight sum, then returns the specified item.

Thereâ€™s a couple of weighted table implementations hanging around on the DevForum. One I found:

Definitely a good method for starters though when youâ€™re getting into weighting. Honestly I donâ€™t know much about it myself and Iâ€™m still figuring out the ropes.

3 Likes