How would I make a card pack? Like you use money and you buy the pack, then you get 5 random items.
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)
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
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.