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.
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.
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
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