Invalid argument #2 to 'random' (interval is empty)

Hi there, I am following the Mad Bloxxer Tutorial on the Roblox Youtube Channel.
I am having an issue with this segment of code:

local AllMaps = Maps:GetChildren()
local NewMap = AllMaps[math.random(1, #AllMaps)]
NewMap.Parent = game.Workspace

It is returning the error “invalid argument #2 to ‘random’ (interval is empty).”
I apologize if I forget anything, I am new to scripting.

AllMaps has less than 1 maps in it. In this case that’s most likely 0. math.random(1, 0) or any similar arrangement where the second argument of math.random() is less than the first will cause this error. The first thing to check is that Maps actually has maps in it. If it does, we can look into some other possibilities.

Such as the very small possibility that the maps haven’t loaded into the game yet. If that is the case, adding a wait to the top of your script should fix it.

wait(3)
local AllMaps = Maps:GetChildren()
etc

This isn’t a great way of handling this just so you know, but the ‘proper’ way to handle this is a little more complicated - such as waiting for one or two players to load in which you would need to play the game anyways - and it’s not worth fussing over at this stage of learning.

1 Like

Adding wait(3) did not fix the issue.

Can I see the full function this is in? Oh also you are positive that this is the line with the error on it? Just want to make sure before we play wild goose chase.

1 Like

Not sure if this is the correct bit:

local IntermissionTime = 20
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")
local Event = ReplicatedStorage:WaitForChild("RemoteEvent")
local Maps = ServerStorage:WaitForChild("Maps")
local MapHolder = game.Workspace:WaitForChild("MapHolder")

while true do
	MapHolder:ClearAllChildren() -- Map Loading
	wait(3)
	local AllMaps = Maps:GetChildren()
	local NewMap = AllMaps[math.random(1, #AllMaps)] -- Line with error
	NewMap.Parent = game.Workspace
	wait(2)
	--
	while true do -- Waiting for Contestants
		wait(5)
		Contestants = {}
		for _, player in pairs(game.Players:GetPlayers()) do
			if player and player.Character then
				local Humanoid = player.Character:WaitForChild("Humanoid")
				if Humanoid and Humanoid.Health > 0 then
					table.insert(Contestants, player)
				end
			end
		end
		if #Contestants >= 2 then
			break
		else
			
		end
	end
	--
	Bloxxer = Contestants[math.random(1, #Contestants)] -- Choosing Bloxxer
	--
	while true do -- Choosing Sheriff
		RandomPlayer = Contestants[math.random(1, #Contestants)] --
		if RandomPlayer ~= Bloxxer then
			Sheriff = RandomPlayer
			break
		end
	end
	--
	local SpawnsModel = NewMap:WaitForChild("Spawns")-- Teleporting Contestants
	local Spawns = SpawnsModel:GetChildren()
	for _, player in pairs(Contestants) do
		if player and player.Character and #Spawns > 0 then
			local UpperTorso = player.Character:WaitForChild("UpperTorso")
			local SpawnIndex = math.random(1, #Spawns)
			local Spawn = Spawns[SpawnIndex]
			if Spawn and UpperTorso then
				table.remove(Spawns, SpawnIndex)
				UpperTorso.CFrame = CFrame.new(Spawn.Position + Vector3.new(0, 3, 0))
				
				local MatchTag = Instance.new("StringValue")
				MatchTag.Name = "MatchTag"
				MatchTag.Parent = player.Character
				
				
				local Backpack = player:FindFirstChild("Backpack") -- Assigning Tools
				if Backpack then
					if player == Bloxxer then
						local Knife = ServerStorage:WaitForChild("Knife"):Clone()
						Knife.Parent = Backpack
					elseif player == Sheriff then
						local Gun = ServerStorage:WaitForChild("Gun"):Clone()
						Gun.Parent = Backpack
					end
				end
			end
		end
	end
	SpawnsModel:Remove()
	--
	local LocalTimer = RoundTimer -- Checking End Conditions
	while LocalTimer > 0 do
		wait(1)
		LocalTimer = LocalTimer - 1
		ActiveContestants = {}
		BloxxerActive = false
		for _, players in pairs(Contestants) do
			if player then
				local Character = player.Character
				if Character then
					local MatchTag = Character:FindFirstChild("MatchTag")
					local Humanoid = Character:FindFirstChild("Humanoid")
					if MatchTag and Humanoid and Humanoid.Health > 0 then
						if player == Bloxxer then
							BloxxerActive = true
						end
						table.insert(ActiveContestants, player)
					end
				end
			end
		end
		if #ActiveContestants <= 1 or not BloxxerActive then
			break
		end
	end
	--
	local GameResult = "PlayersWin"
	if BloxxerActive then
		if #Contestants > 2 then
			-- Bloxer Loses!
		else
			GameResult = "BloxxerWin" -- Bloxxer Wins!
		end
	else
		-- Bloxxer Captured!
	end
	--
	local LobbySpawns = {} -- Returning Players to Lobby
	for _, v in pairs(game.Workspace:WaitForChild("Lobby"):GetChildren()) do
		if v and v.Name == "SpawnLocation" then
			table.insert(LobbySpawns, v)
		end
	end
	for _, player in pairs(ActiveContestants) do
		if player then
			if player.Character then
				local Humanoid = player.Character:FindFirstChild("Humanoid")
				if Humanoid then
					Humanoid:UnequipTools()
				end
			end
			local RandomSpawn = LobbySpawns[math.random(1, #LobbySpawns)]
			player.Character:MoveTo(RandomSpawn.Position)
			local Backpack = player:FindFirstChild("Backpack")
			if Backpack then
				Backpack:ClearAllChildren()
			end
		end
	end
	
	wait(1)
end
1 Like

I don’t see any possibility other than:

Are you sure that your maps are parented correctly?

You never use the variable Maps in the code except for that part so it has to be a problem related to that.

2 Likes

I think they are.
image

Do you have a code changing the contents of Maps? Or, that’s probably not the case but, is your script somehow client sided?

if the code is in a localScript, then it wont work cuz “Maps” is in the serverStorage as cnr pointed out
also try moving the Map models into a Folder called maps instead of another Model like you’ve done

It will NOT work if it is a local script, if it is a server script try adding this at the top of your code

repeat wait() until #game:GetService("ServerStorage"):WaitForChild("Maps"):GetChildren() >= 1

I have found the issue. The tutorial did not tell me I needed to put :Clone() at the end of the local NewMap = AllMaps[math.random(1, #AllMaps)] line.

2 Likes

dont forget to add NewMap.Parent = workspace

2 Likes