How to get a random item out of a table

I keep getting this error: ServerScriptService.Main.RoundScript:31: invalid argument #2 to ‘random’ (interval is empty)

 local availableMaps = {}
    for _, map in pairs(maps:GetChildren()) do
        if map.Settings.Difficulty == flood.intensity then
            table.insert(availableMaps,map)
        end
    end

 local chosen = availableMaps[math.random(1,#availableMaps)]

this is a map script where it picks maps with the same intensity.
if someone can fix this error i will be very thankful

availableMaps length is 0. Thus, it is an invalid 2nd argument for math.random.

The condition in the loop probably wasn’t satisfied, so it didn’t insert a value to the table.

1 Like

I see now, I printed out the table before the random and it just showed “{}”.
How would I go by fixing this?

There are two things I can think of.

#1
There is nothing insinde maps.

#2
The conditional statement you put in the loop wasn’t satisfied, so you could check with a print statement before the condition to debug whether two values are true.

Wait, is difficulty a string value?

Its a number value, I just realised i didnt do .Value

I forgot to put .Value lmao thanks for the help

No problem! If you have any more issues feel free to add.

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