Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

ServerScriptService.Values:19: invalid argument #2 to 'random' (interval is empty) ?

local Rolelist = {
	["Mafia"] = 1,
	["Doctor"] = 2,
	["Jailor"] = 3
}
	local Role = Instance.new("IntValue", plr)
	Role.Name = "Role"
	Role.Value = 0
	local function random2()
		local number = math.random(1, #Rolelist)

		repeat
			number = math.random(1, #Rolelist)
		until (not roleUsed[number])

		roleUsed[number] = true

		Rolelist.Value = number

	end
random2()

What am I doing wrong?

4 comments

0

can you put your full script or a little more? line 19 is blank from what you sent

0

try printing RoleList right before line 19, see what is inside of it.

0

maybe the table is supposed to be something like local Rolelist = {'Mafia', 'Doctor', 'Jailor'} (without the = Number)

0

After testing it, for some reason its counting Rolelist as having zero entries. Maybe cause its a dictionary and not an array, but I don't remember that sort of behaviour being a thing.

2 answers

✓ Accepted answer

I don't believe you've provided the full script; so I don't know where the line 19 quoted in the title is.

I hope maybe by rewriting this section I can help others understand this more.

local RoleUsed = {}
local RoleList = {}

RoleList["Mafia"] = 1
RoleList["Doctor"] = 2
RoleList["Jailer"] = 3

local IntValue = Instance.new("IntValue")
IntValue.Name = "Role"
IntValue.Value = 0
IntValue.Parent = plr

local function random2()
	local n = math.random(1, #RoleList)
	repeat
		n = math.random(1, #RoleList)
	until not RoleUsed[n]
	RoleUsed[n] = true
	IntValue.Value = n
end

random2()

This is the full script

local used = {}
local roleUsed = {}

local Rolelist = {
	["Mafia"] = 1,
	["Doctor"] = 2,
	["Jailor"] = 3
}
game.Players.PlayerAdded:Connect(function(plr)
	
	local Id = Instance.new("IntValue", plr)
	Id.Name = "Id"
	Id.Value = 0
	
	local Role = Instance.new("IntValue", plr)
	Role.Name = "Role"
	Role.Value = 0
	local function random2()
		local number = math.random(1, #Rolelist)

		repeat
			number = math.random(1, #Rolelist)
		until (not roleUsed[number])

		roleUsed[number] = true

		Rolelist.Value = number

	end
	local function random()
		local number = math.random(1, 8)

		repeat
			number = math.random(1, 8)
		until (not used[number])

		used[number] = true
		
		Id.Value = number

	end
	random()
	random2()
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.