Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years 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 1033 — 3y
0
try printing RoleList right before line 19, see what is inside of it. Benbebop 1049 — 3y
0
maybe the table is supposed to be something like local Rolelist = {'Mafia', 'Doctor', 'Jailor'} (without the = Number) BulletproofVast 1033 — 3y
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 1049 — 3y

2 answers

Log in to vote
1
Answered by 3 years ago
Edited 3 years 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 3 years 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