ServerScriptService.GameHandler:404: invalid argument #2 to 'random' (interval is empty) Trying to pick a random player from table

I tried to make a plates of fate game and this is the serverscript.

local DataStore = DataStoreService:GetDataStore("PlatesOfFateXDataStoreTest1")
local GameData = game.ReplicatedStorage.GameData
local Assets = game.ServerStorage.Assets
local Folder = game.Workspace.Folder
local TweenService = game:GetService("TweenService")
local Status = GameData.Status

local Settings = {
	Intermission = 10,
	PlayersNeeded = 2,
	TimeBetweenEvents = 5,
	RoundInProgress = GameData.RoundInProgress,
	GameStartTime = 5
}

local function CalculatePlayers()
	local NumberOfPlayers = #game.Players:GetPlayers()
	return NumberOfPlayers
end

local function SelectBox(Plate)
	local Select = Instance.new("SelectionBox")
	Select.Parent = Plate
	Select.Adornee = Plate
	Select.Color3 = Color3.fromRGB(255, 0, 0)
	game.Debris:AddItem(Select, 1.5)
end

local function SelectPlayer(Player)
	local Select = Instance.new("SelectionBox")
	Select.Parent = Player.Character.HumanoidRootPart
	Select.Adornee = Player.Character.HumanoidRootPart
	Select.Color3 = Color3.fromRGB(255, 0, 0)
	game.Debris:AddItem(Select, 1.5)
end

local function Events(Player)
	local RandomEvent = math.random(1,25)
	local Plate = Player.Character:FindFirstChild("PlateValue").Value

	local Sound = Assets.Alarm:Clone()
	Sound.Parent = game.Workspace
	Sound:Play()
	game.Debris:AddItem(Sound, 5)

	if RandomEvent == 1 then
		Status.Value = Player.Name.. " plate is going to turn Red!"
		Plate.BrickColor = BrickColor.new("Really red")
		SelectBox(Plate)
	elseif RandomEvent == 2 then
		Status.Value = Player.Name.. " plate is going turn into Sand!"
		Plate.Material = "Sand"
		SelectBox(Plate)
	elseif RandomEvent == 3 then
		local Shrink = math.random(1,14)
		Status.Value = Player.Name.. " plate is going to shrink "..Shrink.." studs!"
		local ShrinkInfo = TweenInfo.new(
			13.017,
			Enum.EasingStyle.Linear,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		local Tween = TweenService:Create(Plate, ShrinkInfo, {Size = Plate.Size - Vector3.new(Shrink, 0, Shrink)}):Play()
		SelectBox(Plate)
	elseif RandomEvent == 4 then
		local Grow = math.random(1,20)
		Status.Value = Player.Name.. " plate is going to Grow "..Grow.." studs!"
		local ShrinkInfo = TweenInfo.new(
			13.017,
			Enum.EasingStyle.Linear,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		local Tween = TweenService:Create(Plate, ShrinkInfo, {Size = Plate.Size + Vector3.new(Grow, 0, Grow)}):Play()
		SelectBox(Plate)
	elseif RandomEvent == 5 then
		Status.Value = Player.Name.. " plate will spawn a zombie!"
		local Zombie = Assets.Zombie:Clone()
		Zombie.Parent = game.Workspace.Folder
		Zombie.HumanoidRootPart.CFrame = Plate.CFrame * CFrame.new(3, 2, 0)
		SelectBox(Plate)
	elseif RandomEvent == 6 then
		local RandomBoost = math.random(1,15)
		Status.Value = Player.Name.. " is going to get a speed boost by "..RandomBoost.."!"
		Player.Character.Humanoid.WalkSpeed += RandomBoost
		SelectPlayer(Player)
	elseif RandomEvent == 7 then
		local RandomBoost = math.random(1,15)
		Status.Value = Player.Name.. " is going to get a jump boost by "..RandomBoost.."!"
		Player.Character.Humanoid.JumpPower += RandomBoost
		SelectPlayer(Player)
	elseif RandomEvent == 8 then
		local Sword = Assets.Sword:Clone()
		Sword.Parent = Player.Backpack
		Status.Value = Player.Name.. " Receieved a sword!"
		SelectPlayer(Player)
	elseif RandomEvent == 9 then
		local Grow = math.random(1,15)
		Status.Value = "Every plate will grow by "..Grow.." studs!"
		for key, players in pairs(game.Players:GetPlayers()) do
			if players.Character:FindFirstChild("PlateValue") then
				local TempPlate = players.Character:FindFirstChild("PlateValue").Value
				local ShrinkInfo = TweenInfo.new(
					13.017,
					Enum.EasingStyle.Linear,
					Enum.EasingDirection.Out,
					0,
					false,
					0
				)
				local Tween = TweenService:Create(TempPlate, ShrinkInfo, {Size = TempPlate.Size + Vector3.new(Grow, 0, Grow)}):Play()
				SelectBox(TempPlate)
			end
		end
	elseif RandomEvent == 10 then
		local Shrink = math.random(1,5)
		Status.Value = "Every plate will shrink by "..Shrink.." studs!"
		for key, players in pairs(game.Players:GetPlayers()) do
			if players.Character:FindFirstChild("PlateValue") then
				local TempPlate = players.Character:FindFirstChild("PlateValue").Value
				local ShrinkInfo = TweenInfo.new(
					13.017,
					Enum.EasingStyle.Linear,
					Enum.EasingDirection.Out,
					0,
					false,
					0
				)
				local Tween = TweenService:Create(TempPlate, ShrinkInfo, {Size = TempPlate.Size - Vector3.new(Shrink, 0, Shrink)}):Play()
				SelectBox(TempPlate)
			end
		end
	elseif RandomEvent == 11 then
		local GC = Assets.GravityCoil:Clone()
		GC.Parent = Player.Backpack
		Status.Value = Player.Name.. " Has Receieved A Gravity Coil!"
		SelectPlayer(Player)
	elseif RandomEvent == 12 then
		local GC = Assets.RocketLauncher:Clone()
		GC.Parent = Player.Backpack
		Status.Value = Player.Name.. " Has Receieved A Rocket Launcher!"
		SelectPlayer(Player)
	elseif RandomEvent == 13 then
		local GC = Assets.Pistol:Clone()
		GC.Parent = Player.Backpack
		Status.Value = Player.Name.." Has Received A Pistol"
		SelectPlayer(Player)
	elseif RandomEvent == 14 then
		local DoubleLavaSpinner =  Assets.DoubleLavaSpinner:Clone()
		DoubleLavaSpinner.Parent = game.Workspace.Folder
		DoubleLavaSpinner.CFrame = Plate.CFrame * CFrame.new(0, 1, 0)
		DoubleLavaSpinner.Transparency = 1
		DoubleLavaSpinner.CanCollide = false
		DoubleLavaSpinner.CanTouch = false
		local Tween = TweenService:Create(DoubleLavaSpinner, TweenInfo.new(3, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Transparency = 0})
		Tween:Play()
		Tween.Completed:Connect(function()
			DoubleLavaSpinner.CanCollide = true
			DoubleLavaSpinner.CanTouch = true
		end)
		Status.Value = Player.Name.."s Plate Will Get A Double Lava Spinner"
		SelectPlayer(Player)
	elseif RandomEvent == 15 then
		for key, players in pairs(game.Players:GetPlayers()) do
			if players.Character:FindFirstChild("PlateValue") then
				players.Character.HumanoidRootPart.Anchored = false
				local TempPlate = players.Character:FindFirstChild("PlateValue").Value
		local LavaSpinner = Assets.LavaSpinner:Clone()
		LavaSpinner.Parent = game.Workspace.Folder
		SelectBox(TempPlate)
				for i = 60, 1, -1 do
					if game.ReplicatedStorage.GameData.RoundInProgress.Value == false then
						LavaSpinner:Destroy()
					end
					Status.Value = "Arena: A Lava  Spinner Has Been Added For 1 Minute! Time Left: "..i
			if i == 1 then
				LavaSpinner:Destroy()
			end
			wait(1)
				end
			end
		end
	elseif RandomEvent == 16 then
		Status.Value = "Arena: A Tsunami Is Incomming! EVERYONE WILL DIE!!!!"
		local Tsunami = Assets.TsunamiWave:Clone()
		Tsunami.Parent = game.Workspace.Folder
		Tsunami.CanCollide = false
		local Tween = TweenService:Create(Tsunami, TweenInfo.new(120, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {CFrame = CFrame.new(-8342.447, 124.149, -283.963)})
		Tween:Play()
		Player.Character.HumanoidRootPart.Anchored = false
		Tween.Completed:Connect(function()
			Tsunami:Destroy()
		end)
	elseif RandomEvent == 17 then
		local GC = Assets.GrappleHook:Clone()
		GC.Parent = Player.Backpack
		Status.Value = Player.Name.." Has Received A Grappling Hook"
		SelectPlayer(Player)
	elseif RandomEvent == 18 then
		local Cell = Assets.JailCell:Clone()
		Cell.Parent = game.Workspace.Folder
		Cell.CFrame = Plate.CFrame * CFrame.new(0, 8, 0)
		Status.Value = Player.Name.."s Plate Has Been Jailed"
	elseif RandomEvent == 19 then
		Status.Value = Player.Name.. " plate is going turn into Glass!"
		Plate.Material = "Glass"
		Plate.Reflectance = 0.2
		Plate.BrickColor = BrickColor.new("Baby blue")
		Plate.Transparency = 0.3
		SelectBox(Plate)
	elseif RandomEvent == 20 then
		Status.Value = Player.Name.. " plate is Reverting back to normal!"
		Plate.Material = "SmoothPlastic"
		Plate.Reflectance = 0
		Plate.BrickColor = BrickColor.new("Medium Medium stone grey")
		Plate.Transparency = 0
		local Tween = TweenService:Create(Plate, TweenInfo.new(5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Size = Vector3.new(15, 1, 15)})
		Tween:Play()
	elseif RandomEvent == 21 then
		Status.Value = Player.Name.." Will Freeze Forever"
		Player.Character.HumanoidRootPart.Anchored = true
		elseif RandomEvent == 22 then
		--select multiple random players
		local RandomPlayers = {}
		for i = 1, math.random(2, 5) do
			table.insert(RandomPlayers, game.Players:GetPlayers()[math.random(1, #game.Players:GetPlayers())])
		end
		for key, players in pairs(RandomPlayers) do
			if players.Character:FindFirstChild("PlateValue") then
				local TempPlate = players.Character:FindFirstChild("PlateValue").Value
					Status.Value = players.Name.." Has Been Raised 8 Studs"
				local Tween = TweenService:Create(TempPlate, TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Position = TempPlate.Position + Vector3.new(0, 4, 0)})
					Tween:Play()
			end
		end
	elseif RandomEvent == 23 then
		local GC = Assets.BrickWall:Clone()
		GC.Parent = game.Workspace.Folder
		GC.CFrame = Plate.CFrame * CFrame.new(0, 8.857, 0)
	elseif RandomEvent == 24 then
		local GC = Assets.HealPad:Clone()
		GC.Parent = game.Workspace.Folder
		GC.CFrame = Plate.CFrame * CFrame.new(0, 0.1, 0)
	elseif RandomEvent == 25 then
		local Lava = Assets.Lava:Clone()
		Lava.Parent = game.Workspace.Folder
		
		for key, players in pairs(game.Players:GetPlayers()) do
			if players.Character:FindFirstChild("PlateValue") then
				local TempPlate = players.Character:FindFirstChild("PlateValue").Value
				players.Character.HumanoidRootPart.Anchored = false
			end
		end
		Status.Value = "ARENA: LAVA WILL RISE UNTIL EVERYONE IS DEAD!!!!!"
		for key, players in pairs(game.Players:GetPlayers()) do
			if players.Character:FindFirstChild("PlateValue") then
				local TempPlate = players.Character:FindFirstChild("PlateValue").Value
				SelectBox(TempPlate)
			end
		end
		local crushinfo = TweenInfo.new(
			60, -- Time animating
			Enum.EasingStyle.Linear, -- EasingStyle
			Enum.EasingDirection.Out, -- EasingDirection
			0, -- Repitions
			false, -- Reverse post tween?
			0 -- Delay time
		)

		local crushgoal = {
			Position = Lava.Position + Vector3.new(0, 30, 0)
		}

		local crush = TweenService:Create(Lava, crushinfo, crushgoal)

		wait(5)
		crush:play()
	end
end

while wait() do
	if CalculatePlayers() >=  Settings.PlayersNeeded then
		for i, v in pairs(Folder:GetChildren()) do
			if v then
				v:Destroy()
			end
		end
		local PlayersInGame = {}
		-- The game actually starts
		for i = Settings.Intermission, 1, -1 do
			Status.Value = "Intermission "..i
			wait(1)
		end
		for  i = Settings.GameStartTime, 1, -1 do
		Status.Value = "All Players Will Get Teleported In "..i.." Seconds!"
		Settings.RoundInProgress.Value = true
			wait(1)
		end

		local Plates = Assets.Plates:Clone()
		Plates.Parent = game.Workspace.Folder

		for keyvalue, player in pairs(game.Players:GetPlayers()) do
			if player.Character:FindFirstChild("Humanoid") then
				if player.Character.Humanoid.Health >= 1 then
					table.insert(PlayersInGame, keyvalue, player)
					player.Team = game.Teams.Playing
					player.Character.Humanoid.Died:Connect(function()
						for i = 1, #PlayersInGame do
							if PlayersInGame[i] == player then
								SelectBox(player.Character:FindFirstChild("PlateValue").Value)
								local TransInfo = TweenInfo.new(
									2,
									Enum.EasingStyle.Linear,
									Enum.EasingDirection.Out,
									0,
									false,
									0
								) 
								local Tween = TweenService:Create(player.Character:FindFirstChild("PlateValue").Value, TransInfo, {Transparency = 1}):Play()
								coroutine.wrap(function()
									wait(2)
									player.Character:FindFirstChild("PlateValue").Value:Destroy()
								end)()
								table.remove(PlayersInGame, i)
								if player.Team == game.Teams.Playing then
									player.Team = game.Teams.Lobby
								end
							end
						end
					end)
					for i, v in pairs(Plates:GetChildren()) do
						if v.Settings.OwnerOfPlate.Value == "None" and not player.Character:FindFirstChild("PlateValue") then
							v.Settings.OwnerOfPlate.Value = player.Name
							player.Character.HumanoidRootPart.CFrame = v.CFrame * CFrame.new(0,2,0)
							local PlateValue = Instance.new("ObjectValue")
							PlateValue.Parent = player.Character
							PlateValue.Value = v
							PlateValue.Name = "PlateValue"
						end
					end
				end
			end
		end

		for i, v in pairs(Plates:GetChildren()) do
			if v.Settings.OwnerOfPlate.Value == "None" then
				v:Destroy()
			end
		end

		game.Players.PlayerRemoving:Connect(function(Player)
			for i = 1, #PlayersInGame do
				SelectBox(Player.Character:FindFirstChild("PlateValue").Value)
				local TransInfo = TweenInfo.new(
					2,
					Enum.EasingStyle.Linear,
					Enum.EasingDirection.Out,
					0,
					false,
					0
				) 
				local Tween = TweenService:Create(Player.Character:FindFirstChild("PlateValue").Value, TransInfo, {Transparency = 1}):Play()
				coroutine.wrap(function()
					wait(2)
					Player.Character:FindFirstChild("PlateValue").Value:Destroy()
				end)()
				if PlayersInGame[i] == Player then
					table.remove(PlayersInGame, i)
				end
			end
		end)
		
		for i = 5, 1, -1 do
			Status.Value = "The Game Will Start In "..i.." Seconds"
			wait(1)
		end
		repeat
			if #PlayersInGame >= 2 then
				for i = Settings.TimeBetweenEvents, 1, -1 do
					Status.Value = "Next Event Is In "..i.. " Seconds!"
					wait(1)
				end
				if #PlayersInGame >= 2 then
					local Value = math.random(1, #PlayersInGame)
					local RandomPlayer = PlayersInGame[Value]

					Events(RandomPlayer)
					wait(2.5)
				else
					wait(5)
					for i, v in pairs(PlayersInGame) do
						table.remove(PlayersInGame, i)
					end
				end
			else
				Settings.RoundInProgress.Value = false
				if #PlayersInGame <= 1 then
					local Value = math.random(1, #PlayersInGame)
					local RandomPlayer = PlayersInGame[Value]
					if Value ~= nil then
						Status.Value = RandomPlayer.Name.." has won."
						for i, v in  pairs(game.Workspace.Folder:GetChildren()) do
							v:ClearAllChildren()
						end 
						wait(5)
						RandomPlayer.leaderstats.Wins.Value += 1
						RandomPlayer.leaderstats.Coins.Value += 12000
					end
				else
					Status.Value = "No one won!"
					wait(5)
				end
			end
		until Settings.RoundInProgress.Value == false
	else
		-- The game doesnt have enough players
		Status.Value = "Need more players to begin..."
	end
end```
can anyone help me?
1 Like

Well, can you tell us which line the error is coming from?

it has something to do with the picked winner inside the table, right here;

			if #PlayersInGame >= 2 then
				for i = Settings.TimeBetweenEvents, 1, -1 do
					Status.Value = "Next Event Is In "..i.. " Seconds!"
					wait(1)
				end
				if #PlayersInGame >= 2 then
					local Value = math.random(1, #PlayersInGame)
					local RandomPlayer = PlayersInGame[Value]

					Events(RandomPlayer)
					wait(2.5)
				else
					wait(5)
					for i, v in pairs(PlayersInGame) do
						table.remove(PlayersInGame, i)
					end
				end
			else
				Settings.RoundInProgress.Value = false
				if #PlayersInGame <= 1 then
					local Value = math.random(1, #PlayersInGame)
					local RandomPlayer = PlayersInGame[Value]
					if Value ~= nil then
						Status.Value = RandomPlayer.Name.." has won."
						for i, v in  pairs(game.Workspace.Folder:GetChildren()) do
							v:ClearAllChildren()
						end 
						wait(5)
						RandomPlayer.leaderstats.Wins.Value += 1
						RandomPlayer.leaderstats.Coins.Value += 12000
					end
				else
					Status.Value = "No one won!"
					wait(5)
				end
			end
		until Settings.RoundInProgress.Value == false
	else
		-- The game doesnt have enough players
		Status.Value = "Need more players to begin..."
	end
end```
1 Like

are you there or are you currently out? (I’ll wait)

1 Like

That error occurs when the minimum value is greater than the maximum value. Looking at the line you’re referring to I don’t see anything wrong with it.

The only thing I can think of is if #PlayersInGame is 0.

Where is the code that determines the amount of players in the game? It might be an issue with that.

1 Like

But then how do i change it so that it actually picks the player before they get removed from the table?

I am unsure. I will play around with this code when I get the chance and get back to you

okay thanks but did you find anything out yet?

Replace if #PlayersInGame <= 1 then to if #PlayersInGame == 1 then
Because if #PlayersInGame is 0, this will fire and try math.random(1, 0), which will throw an error.

it still doesnt work as it just do not do the next even in 5 seconds it just pauses the script and completely pauses the game. the game does not end and the player is stuck on his own plate.