Ad
Log in to vote
0

"invalid argument #2 to 'random' (interval is empty)" problem?

Asked by 16 days ago

Whenever I test-play my Hide and Seek game, I always see this error message appear: "invalid argument #2 to 'random' (interval is empty)". How do I solve this issue?

Full Code:

-- Services
local replicatedStorage = game:GetService("ReplicatedStorage")
local serverStorage = game:GetService("ServerStorage")

local status = replicatedStorage:WaitForChild("Values").Status

-- Config
local playersToStart = 1

-- Functions

local function chooseSeeker(availablePlayers)
    return availablePlayers[math.random(1, #availablePlayers)]
end

local function teleportPlayers(availablePlayers, spawns) -- availablePlayers: Table / spawns: Table
    for _, plr in pairs(availablePlayers) do
        if plr.Character then
            if plr.Character:FindFirstChild("HumanoidRootPart") then
                plr.Character.HumanoidRootPart.CFrame = spawns[math.random(1, #spawns)].CFrame + Vector3.new(0,5,6) <-- line with error message
            end
        end
    end
end

while wait(1) do

    repeat

        print("Not enough players in-game!")
        status.Value = playersToStart.." players or more are needed to start. (/"..playersToStart..")"

        wait(1)

    until #game.Players:GetPlayers() >= playersToStart

    for i = 5,0,-1 do
        status.Value = "Next round starts in "..i.." seconds."
        wait(0.1)
    end

    local contestants = game.Players:GetPlayers()

    local seeker = chooseSeeker(contestants)

    status.Value = "The chosen seeker is... "
    wait(2)
    status.Value = seeker.Name.."!"

    wait(3)

    local map = workspace.Map

    teleportPlayers(contestants, map.Spawns:GetChildren())
    wait(5)
end
0
I'm not sure but I don't THINK # can be used in Lua coding theking66hayday 148 — 16d
0
# can be used in Lua. TimEntity 0 — 16d
0
Oh i didn't know that thanks theking66hayday 148 — 16d
0
Quick Question what line does it say it has the error on the output on. theking66hayday 148 — 16d
View all comments (2 more)
0
The line it says it has the error on the output on is Line 20. TimEntity 0 — 16d
0
Can't test right now so I don't know if this helps or not, you may already know this but pretty sure that the game is detecting that you have no spawn locations. Maybe test with printing spawns to see what it says. BulletproofVast 901 — 15d

1 answer

Log in to vote
0
Answered by 11 days ago

According to internet research, you would get an "interval is empty" error if the second argument is less than the first argument. Your spawns table may be empty at times, causing this error; check if this is the case.

Ad

Answer this question