Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Interval is empty. Nobody can find the solution. Help?

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)

2 comments

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())

0

dude, this question has been posted more than 3 times, I remember at least 15 posts like this

2 answers

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()

1 comment

0

Thanks! You saved my 12 hours. I am stupid enough tho! Im only a beginner. Major thx!

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

8 comments

0

9: bad argument #1 to 'random' (interval is empty). Error on local num = math.random

0

that shouldn't happen with at least 1 player

0

I join the game, it happens

0

try now after the edit i made

0

Prints out 0 and gives 8: bad argument #2 to 'random' (interval is empty)

0

error @ local num = math.random(1, #game.Players:GetPlayers())

0

Change "if #players ~= 1" to "if #players >= 1", and remove "else selected = players[1]", but keep "end"

0

argument 1 invalid or nil?

This discussion is preserved as it was. New answers, comments, and votes are closed.