Ad
Log in to vote
0

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

Asked by 6 days ago
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?

0
can you put your full script or a little more? line 19 is blank from what you sent BulletproofVast 370 — 6d
0
try printing RoleList right before line 19, see what is inside of it. Benbebop 976 — 6d
0
maybe the table is supposed to be something like local Rolelist = {'Mafia', 'Doctor', 'Jailor'} (without the = Number) BulletproofVast 370 — 6d
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. Benbebop 976 — 6d

2 answers

Log in to vote
0
Answered by 6 days ago
Edited 6 days ago

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()
Ad
Log in to vote
0
Answered by 1 day ago

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)

Answer this question