Invalid Arguement #2 to 'random' (interval is empty)

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

  1. What do you want to achieve? Keep it simple and clear!
    Im trying to make a table with properties of each ghost which then randomly selects one of them (im making an exorcist game)
  2. What is the issue? Include screenshots / videos if possible!
    im getting the error “Invalid Arguement #2 to ‘random’ (interval is empty)”
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    nothing ive seen directly applies to what im doing
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local ghostTypes = {

	["Yurei"] = {
		["Speed"] = 1.1,
		["Agression"] = 2,
		["Blinktime"] = 0.5,
		["Fuse"] = 1,
		["Sanity"] = 50,
		["Failkill"] = 40, -- chance of it killing you when you fail to kill it
	},
	["Onryo"] = {
		["Speed"] = 1,
		["Agression"] = 3,
		["Blinktime"] = 0.9,
		["Fuse"] = 1.5,
		["Sanity"] = 60,
		["Failkill"] = 30, 
	},
	["Yokai"] = {
		["Speed"] = 1,
		["Agression"] = 0.75,
		["Blinktime"] = 0.7,
		["Fuse" ]=2,
		["Sanity"]= 70, 
		["Failkill"] = 20, 
	},
	["Marid"] = {
		["Speed"] = 1.4,
		["Agression"] = 1,
		["Blinktime"] = 1.1,
		["Fuse" ] = 3 ,
		["Sanity"] = 80, 
		["Failkill"] = 10,
	},
	["Shiryō"] = {
		["Speed"] = 0.5,
		["Agression"] = 3,
		["Blinktime"] = 1.1,
		["Fuse" ] = 0.1,
		["Sanity"] = 75, 
		["Failkill"] = 70,
	}

}

local ghostnumber = math.random(1, #ghostTypes)
local randomGhost = ghostTypes[ghostnumber]

print(randomGhost)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

This won’t work, because the table is not ordinal:

local ghostnumber = math.random(1, #ghostTypes)

You should instead do the following:

local function GetKeys(tbl)
  local keys = {}
  for i, v in pairs(tbl) do
    table.insert(keys, i)
  end
  return keys
end
local function Sample(tbl)
  local keys = GetKeys(tbl)
  local key = keys[math.random(1, #keys)]
  return key, tbl[key]
end

local randomGhostName, randomGhost = Sample(ghostTypes)

print(randomGhostName, randomGhost)
2 Likes

I was expecting the # operator to work for a dictionary, same as you. But it seems that it does not.

https://devforum.roblox.com/t/table-with-a-dictionary/1184562

1 Like

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