Getting a random pack not working

Hey!

I have a system set up which should print a list of 3 random packs. I don’t know why it’s not working. No errors but “Called” is not printing and this prints to the console: {
[1] = “Kakashi S1”,
[2] = “Naruto S1”,
[3] = “Sasuke S1”,
[4] = “Gaara S1”,
[5] = “Sakura S1”,
[6] = “Sasuke S1”
} - Server - CardManager:113

Script:

local Players = game:GetService("Players")

local PacksModule = require(script:WaitForChild("Packs"))
local Config = require(script:WaitForChild("Config"))

local ChosenPacks = {}

local function GetPack()
	
	print("Called")
	local pack = PacksModule.Packs[math.random(1, #PacksModule.Packs)]
	
	if ChosenPacks[pack] then
		GetPack()
	else
		table.insert(ChosenPacks, pack)
	end
	
	return pack
end

local function GetAllPacks ()
	
	local NumberOfPacks = Config.Config["NumberOfPacks"]
	
	for i = 1, NumberOfPacks, 1 do
		
		GetPack()		
		
	end
	
	print(ChosenPacks)
	
end

I’m not sure how PacksModule works, but there might be an issue with your condition checking for ChosenPacks[pack]. Try if table.find(ChosenPacks, pack) then instead.

This is it:

local module = {}

module.Packs = {
	
	["Naruto Pack"] = {
		
		["Card1"] = "Naruto S1",
		["Card2"] = "Sasuke S1",
		["Card3"] = "Sakura S1",
		["Card4"] = "Kakashi S1",
		["Card5"] = "Gaara S1"
		
	},
	
	["One Piece Pack"] = {
		
		["Card1"] = "Luffy S1",
		["Card2"] = "Zoro S1",
		["Card3"] = "Nami S1",
		["Card4"] = "Robin S1",
		["Card5"] = "Sanji S1"
		
	},
	
	["Dragon Ball Pack"] = {
		
		["Card1"] = "Goku S1",
		["Card2"] = "Gohan S1",
		["Card3"] = "Krillin S1",
		["Card4"] = "Piccolo S1",
		["Card5"] = "Vegeta S1"
		
	},
	
}

return module

I might be misinterpreting this, but that’s a dictionary. Attempting to use math.random for a random numerical index will not work with it.

If you wanted it to be random, you’d have to introduce a kind of workaround for it.

local packthings = {"Naruto Pack", "One Piece Pack", "Dragon Ball Pack"}

...

local pack = PacksModule.Packs[packthings[math.random(1, #paththings)]]

Then for the cards

local card = pack["Card" .. math.random(1, 5)]
2 Likes

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