Trying to get a Value from a Table

Ive been trying for a bit to see how to print whats selected in a table by random. i keep getting the outpur error: ServerScriptService.GameModeTable:25: invalid argument #2 to ‘random’ (interval is empty). Is there i way i can use Table.find or table.foreach?

   local gamemodes = {

	TDM = {
	
		Time = ": 60",	
	
			Respawn = true,
			
				pointLimit = ": 20",
	
	
			};

FreeForAll = {
	
	Time = ": 60",	

		Respawn = false,
			
			pointLimit = ": None",
      }

}
									
  local chosenGM = math.random(1,#gamemodes)
               print(chosenGM)
2 Likes

You can get the dictionary length by looping over the keys. You cannot get the length of a dictionary using #.

2 Likes

Well with your Error its being caused because the upper limit is lower than your lower limit

So

#gamemodes

Is less than or equal to 1
So math.random will always equal 1
this is because it is a dictionary, dictionaries are not able to be counted using “#table
Try something like this

local Amount = 0
for I,_ in pairs(tablehere) do
    Amount = Amount + 1
end
local choosenGM = math.random(1,Amount)
print(choosenGM)
3 Likes

what im trying to do is for Math.random to select from the table a gamemode and then i want to
print(ie:“FreeForAll”) not the number of GameModes in the Table.

Sorry i must have not worded it correctly. Im trying to get print to print the randomly selected gamemode in text by name.

Create a separate table that contains all of the keys within the dictionary, that looks like this:

local gameModesKeys = {"TDM", "FreeForAll"}

Then, using starnova224’s example, you would get the key by doing the following:

local randomKey = gameModesKeys[choosenGM]

1 Like

Ah well when you print math.random() it will print a number

So you could use

local Amount = 0
for I,_ in pairs(tablehere) do
    Amount = Amount + 1
end
local randomGM = math.random(1,Amount)
print(table[randomGM])

also if you try to print a dictionary i believe it will throw an error(or print the memory address i cant remember which)
so if you want to print the name i believe its…

print(table[randomGM].key)

i might be wrong though

simple.

local chosenGM = gamemodes[math.random(1,#gamemodes)
print(chosenGM)
1 Like

That does not work in this case because indexing a dictionary with a number will return nil.

yeah that doesnt work cause im not searching for a number error:
ServerScriptService.GameModeTable:25: invalid argument #2 to ‘random’ (interval is empty)

hmm i think i have a solution you could make keys for each gamemode in the Dictionary

so basically something like this

local randomNum = math.random(1,theamountofgamemodes) -- you have to put the amount in manually
if randomNum == 1 then
    choosenGM = gamemodes.TDM
elseif randomNum == 2 then
    choosenGM = gamemodes.FreeForAll
end
-- you also need to edit the amount of if statements for each new gamemode

i cant really think of any other way to do it

No error but in the output it says: nil.
Also tried the print(table[randomGM].key), got attempted to index nil with 'key'

I guessed that did you try my most recent post? it should work as it has for me multiple times in the past

Also you cant print a dictionary you need to print a value so an example would be something like this

print(gamemodes.TDM.Time)

ive tried similar to your most recent suggestion.

The one way i can print whats in gamemodes, was

    table.foreach(gamemodes,warn)

   table.foreach(gamemodes.TDM,warn)

I was trying to see if there was a way to print the selected random gamemode.

Take a look at my suggestion above.

well with my most recent answer you could use the if statements to print what gamemode it is

if randomNum == 1 then
    print("TDM Selected")
    -- Add extra if statements as needed
end

Speaking of which i really like this answer
Bit more cleaner than mine but idk if it solves the problem(if its not already been solved)

2 Likes