How Would I Choose a Random Value?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I would like to have a function inside my ModuleScript to choose a random card.

  2. What is the issue? I can’t figure out how to choose a random value from the array!

  3. What solutions have you tried so far? I looked for solutions and tried basically all solutions that I could find but none of them worked.

Here’s my module

local cards = 
{

	--Special
	-- Aces
	["Ace Of Spades"] = {["Value"] = 1},
	["Ace Of Hearts"] = {  ["Value"] = 1 },
	["Ace Of Diamonds"] = {  ["Value"] = 1 },
	["Ace Of Clubs"] = {  ["Value"] = 1},
	-- Kings
	["King Of Spades"] = {  ["Value"] = 10 },
	["King Of Hearts"] = {  ["Value"] = 10 },
	["King Of Diamonds"] = {  ["Value"] = 10 },
	["King Of Clubs"] = {  ["Value"] = 10 },
	-- Queen
	["Queen Of Spades"] = {  ["Value"] = 10 },
	["Queen Of Hearts"] = {  ["Value"] = 10 },
	["Queen Of Diamonds"] = {  ["Value"] = 10 },
	["Queen Of Clubs"] = {  ["Value"] = 10 },
	-- Jacks
	["Jack Of Spades"] = {  ["Value"] = 10 },
	["Jack Of Hearts"] = {  ["Value"] = 10 },
	["Jack Of Diamonds"] = {  ["Value"] = 10 },
	["Jack Of Clubs"] = {  ["Value"] = 10 },

	-- Clubs Cards
	["1 of Clubs"] = {  ["Value"] = 1 },
	["2 of Clubs"] = {  ["Value"] = 2 },
	["3 of Clubs"] = {  ["Value"] = 3 },
	["4 of Clubs"] = {  ["Value"] = 4 },
	["5 of Clubs"] = {  ["Value"] = 5 },
	["6 of Clubs"] = {  ["Value"] = 6 },
	["7 of Clubs"] = {  ["Value"] = 7 },
	["8 of Clubs"] = {  ["Value"] = 8 },
	["9 of Clubs"] = {  ["Value"] = 9 },
	["10 of Clubs"] = {  ["Value"] = 10 },
	-- Spades Cards
	["1 of Spades"] = {  ["Value"] = 1 },
	["2 of Spades"] = {  ["Value"] = 2 },
	["3 of Spades"] = {  ["Value"] = 3 },
	["4 of Spades"] = {  ["Value"] = 4 },
	["5 of Spades"] = {  ["Value"] = 5 },
	["6 of Spades"] = {  ["Value"] = 6 },
	["7 of Spades"] = {  ["Value"] = 7 },
	["8 of Spades"] = {  ["Value"] = 8 },
	["9 of Spades"] = {  ["Value"] = 9 },
	["10 of Spades"] = {  ["Value"] = 10 },
	-- Hearts Cards
	["1 of Hearts"] = {  ["Value"] = 1 },
	["2 of Hearts"] = {  ["Value"] = 2 },
	["3 of Hearts"] = {  ["Value"] = 3 },
	["4 of Hearts"] = {  ["Value"] = 4 },
	["5 of Hearts"] = {  ["Value"] = 5 },
	["6 of Hearts"] = {  ["Value"] = 6 },
	["7 of Hearts"] = {  ["Value"] = 7 },
	["8 of Hearts"] = {  ["Value"] = 8 },
	["9 of Hearts"] = {  ["Value"] = 9 },
	["10 of Hearts"] = {  ["Value"] = 10 },
	-- Diamonds
	["1 of Diamonds"] = {  ["Value"] = 1 },
	["2 of Diamonds"] = {  ["Value"] = 2 },
	["3 of Diamonds"] = {  ["Value"] = 3 },
	["4 of Diamonds"] = {  ["Value"] = 4 },
	["5 of Diamonds"] = {  ["Value"] = 5 },
	["6 of Diamonds"] = {  ["Value"] = 6 },
	["7 of Diamonds"] = {  ["Value"] = 7 },
	["8 of Diamonds"] = {  ["Value"] = 8 },
	["9 of Diamonds"] = {  ["Value"] = 9 },
	["10 of Diamonds"] = {  ["Value"] = 10 }
}
		



return cards




Thank you for helping me!

1 Like

Well, you can’t use number indexes on a dictionary so math.random doesn’t work. I recommend just changing the table keys to numbers, since the values themselves are already tables. Alternatively, you can make another table that does this instead, but I don’t really see a point.

local num = 0
local numberTbl = {}
for i,v in pairs(cards) do
    num +=1
	v.Name = i
	numberTbl[num] = v
end
print(numberTbl[math.random(1,#numberTbl)])
2 Likes

I’ll switch them to numbers, thanks!

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