Script fails when an invalid string get sent in queue system

The code that handles the queue breaks when the user tries to queue a gamemode not listed in the Gamemodes table. I would love help with this because it causes the whole game to stop.

--Queue Handler--
local queue = {}

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		if player.UserId == game.CreatorId then
			if message:sub(1, 7) == "/queue " then
				local Gamemode = message:sub(8)
				table.insert(queue, Gamemode)
				print("Gamemode '" .. Gamemode .. "' has been added to the queue.")
			end
		end
	end)
end)


--Gamemode Picker--
function selectRandomGamemode()
	Gamemodes = {
["Gamemode1"] = 0.1,
["Gamemode2"] = 0.1,
["Gamemode3"] = 0.1 -- Add or remove gamemodes here. The 0.1 is the chance of the gamemode.
	}
	local Weight = 0
	for _, Chance in pairs(Gamemodes) do
		Weight += (Chance * 10)
	end
	local ranNumber = math.random(1, Weight)
	Weight = 0
	for Gamemode, Chance in pairs(Gamemodes) do
		Weight += (Chance * 10)
		if Weight >= ranNumber then
			return Gamemode
		end
	end
end	

function getNextGamemode()
	if next(queue) then
		local Gamemode = table.remove(queue, 1)
		ChosenGamemode = game.Lighting.Gamemodes[Gamemode]
		print("The next gamemode from the queue is: "..Gamemode)
		return Gamemode
	else
		return selectRandomGamemode()
	end
end


--Main Code---
local MapsFolder = game.Lighting.Maps
local Maps = MapsFolder:GetChildren()
wait(7)
while true do
	local ChosenMap = Maps[math.random(1, #Maps)]
	local MapClone = ChosenMap:Clone()
	game.ReplicatedStorage.TimerMessageVisible.Value = true
	game.ReplicatedStorage.TimerMessageText.Value = "Intermission"
	timeEvent = game.ReplicatedStorage.TimeEvent
	function Timer()

		local timeAmount = 15

		while timeAmount > 0 do

			timeEvent:FireAllClients(timeAmount)

			wait(1)

			timeAmount -= 1

		end

	end
	Timer()
	game.ReplicatedStorage.TimerMessageVisible.Value = false
	MapClone.Parent = game.Workspace.Map
	MapClone:MakeJoints()	
	wait(2)
	startNewRound()
	for i, v in pairs(game.Players:GetPlayers()) do
		local randomSpawn = MapClone.TeleportSpots:GetChildren()[Random.new():NextInteger(1, #MapClone.TeleportSpots:GetChildren())]
		v.Character.HumanoidRootPart.CFrame = randomSpawn.CFrame
	end
	Gamemode = getNextGamemode()
	GamemodeClone = game.Lighting.Gamemodes[Gamemode]:Clone()
	if GamemodeClone.Name == "Gamemode1" then
--code here--
elseif GamemodeClone.Name == "Gamemode2" then
--code here--
elseif GamemodeClone.Name == "Gamemode3" then
--code here--
end
1 Like

You should just check if the gamemode that the player queues exists in the dictionary.

-- First move the Gamemodes dictionary outside of the function so we're able to access it
Gamemodes = {
     ["Gamemode1"] = 0.1,
     ["Gamemode2"] = 0.1,
     ["Gamemode3"] = 0.1,
}
local queue = {}

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		if player.UserId == game.CreatorId then
            -- We then verify that the Gamemodes table has a key that matches what the player typed 
			if message:sub(1, 7) == "/queue " and Gamemodes[message:sub(8)] then
				local Gamemode = message:sub(8)
				table.insert(queue, Gamemode)
				print("Gamemode '" .. Gamemode .. "' has been added to the queue.")
			end
		end
	end)
end)

-- Rest of code
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.