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

Selecting random player, error, why?

Asked by 10 years ago

Hi guys, I'm TheLorizz and I don't know why but the math.randoms I put doesn't work, why? This is the first time! I need to select a traitor and a detective from the innocents array, but the output gives me this on "math.random()" lines: bad argument #2 to 'random' (interval is empty)

This is the code:

local players = game.Players:GetChildren()
local innocents = {}
local traitor
local detective

function findType(p)
    local td = p:FindFirstChild("TempData")
    local type = td:FindFirstChild("Type")
    return type
end

_G.startGame = function()
    local h = Instance.new("Hint", game.Workspace)
    h.Text = "Selecting traitor!"
    wait(2)
    for i, player in pairs (players) do
        table.insert(innocents, player)
        local type = findType(player)
        type.Value = "Innocent"
        wait()
    end
    wait(1)
    traitor = innocents[math.random(1, #innocents)]
    local ttype = findType(traitor)
    ttype.Value = "Traitor"
    table.remove(innocents, traitor)
    h.Text = "Traitor selected:" .. " " .. traitor
    wait(1)
    h.Text = "Selecting detective!"
    detective = innocents[math.random(1, #innocents)]
    local dtype = findType(detective)
    dtype.Value = "Detective"
    table.remove(innocents, detective)
    h.Text = "Detective selected:" .. " " .. detective
    wait(1)
    h.Text = "Game started!"
end

Why? I repeat, this is the first time!

1 answer

Log in to vote
1
Answered by 10 years ago

That happens when you have empty tables. Why that happens? Essentially this is what happens: math.random(1, 0) and Lua doesn't like that. Make sure the tables aren't empty.

0
local players = game.Players:GetChildren() that's why! I don't update this variable, THANKS! alessandro112 161 — 10y
Ad

Answer this question