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

Interval is empty. Nobody can find the solution. Help? [closed]

Asked by 6 years ago

This question already has an answer here:

Why is this random 1 person team selector not working?

Got help from very helpful dudes, and this is what left that they could not figure out:

The error that is getting spit out is - 8: bad argument #1 to 'random' (interval is empty)

TeamName = "Monster"
players = game.Players:GetPlayers()
selected = ""

function SelectPlayer()
    if #players ~= 1 then
        print(#players)
        local num = math.random(game.Players.NumPlayers)
        selected = players[num]
    else selected = players[1] end
end

SelectPlayer()

function TeamPlayer(player)
    local play = game.Players:FindFirstChild(player.Name)
    local team = game.Teams:FindFirstChild(TeamName)

    if play~= nil then
        play.TeamColor = team.TeamColor
    end
end

TeamPlayer(selected)
0
I'm pretty sure you just asked this question, and it got 3 answers. https://scriptinghelpers.org/questions/54057/why-is-this-random-1-person-team-selector-not-working . It's not working because #players is 0, because you defined it at the beginning, when there were no players. Use: local num = math.random(1,#game.Players:GetPlayers()) UgOsMiLy 1074 — 6y
0
dude, this question has been posted more than 3 times, I remember at least 15 posts like this greatneil80 2647 — 6y

Marked as Duplicate by Vulkarin, UgOsMiLy, TheeDeathCaster, Goulstem, cabbler, and lukeb50

This question has been asked before, and already has an answer. If those answers do not fully address your question, then please ask a new question here.

Why was this question closed?

2 answers

Log in to vote
1
Answered by
Vulkarin 581 Moderation Voter
6 years ago

Please don't say "Nobody can find the solution" when 2 people told you the solution on your previous post

Your script obviously will not work without a player, I setup a minimum amount because it's not useful at all to pick a "random player" with only one player in the game

local minimum = 3 -- The minimum amount of players needed to start a game
local playerServ = game:GetService("Players")
while #playerServ:GetPlayers() < minimum do wait(0.5) end

local function SelectPlayer()
    local players = playerServ:GetPlayers()
    if #players < minimum then return end
    local random = players[Random.new():NextInteger(1, #players)]
    random.Team = game.Teams.someTeam
end

SelectPlayer()
0
Thanks! You saved my 12 hours. I am stupid enough tho! Im only a beginner. Major thx! UncleBenss -3 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago
function SelectPlayer()
    if #players ~= 1 then
        print(#players)
    math.randomseed(math.random(tick()))
        local num = math.random(1, #game.Players:GetPlayers())
        selected = players[num]
    else selected = players[1] end
end
0
9: bad argument #1 to 'random' (interval is empty). Error on local num = math.random UncleBenss -3 — 6y
0
that shouldn't happen with at least 1 player SebbyTheGODKid 198 — 6y
0
I join the game, it happens UncleBenss -3 — 6y
0
try now after the edit i made SebbyTheGODKid 198 — 6y
View all comments (4 more)
0
Prints out 0 and gives 8: bad argument #2 to 'random' (interval is empty) UncleBenss -3 — 6y
0
error @ local num = math.random(1, #game.Players:GetPlayers()) UncleBenss -3 — 6y
0
Change "if #players ~= 1" to "if #players >= 1", and remove "else selected = players[1]", but keep "end" UgOsMiLy 1074 — 6y
0
argument 1 invalid or nil? UncleBenss -3 — 6y