Problem with Round Script

My Round script breaks when playing with the minimum number of players (2) and one player disconnects while playing a round.

When this happens, the 1 remaining player returns to the lobby, the win case text is displayed, but does not advance to “waiting for more payers to join” state.

Based on the error I get, the game is actually stuck at the sortTeams function.
The error says “Failed to set the (player name) team because the player left the game.”

Any ideas on how to fix this? I assume I’m missing something disconnect related.

Here’s the round script.

local teams = game:GetService("Teams")
local roundtime = 360
local inter = 20
local status = game.ReplicatedStorage:WaitForChild("Status")
local maps = game.Lighting.Maps:GetChildren()
local currentmap = workspace:WaitForChild("CurrentMap")
local chosenmap = script:WaitForChild("ChosenMap")
local spawner = workspace.Lobby.Spawn


function chooseMap()
	local choices = {}
	for i = 1, #maps do
		if maps[i]:IsA("Model") then
			table.insert(choices, maps[i])
		end
	end
	local picked = math.random(1,#maps)
	chosenmap.Value = choices[picked].Name
end

function loadMap()
	local map = game.Lighting.Maps:FindFirstChild(chosenmap.Value):Clone()
	map.Parent = currentmap
	--game.ReplicatedStorage.MapPicked:FireAllClients(chosenmap.Value) (text to tell the player what map it is)
end

function deleteMap()
	for i,v in pairs(currentmap:GetChildren()) do
		if v:IsA("Model") then
			v:Destroy()
		end
	end
end

function teleportPlayers()
	local players = game.Players:GetPlayers()
	for i,v in pairs(players) do
		 if v.Team == game.Teams["FirstResponder"] then
        	v.Character.HumanoidRootPart.CFrame = currentmap:FindFirstChild(chosenmap.Value).Spawn.CFrame
    	else
        	v.Character.HumanoidRootPart.CFrame = currentmap:FindFirstChild(chosenmap.Value).Spawn2.CFrame
		end
	end
end



function teleportBack()
	local players = game.Players:GetPlayers()
	for i,v in pairs(players) do
		v.Character.HumanoidRootPart.CFrame = spawner.CFrame
	end
end


function removeItems()
    local Players = game:GetService("Players")
    for _, Player in pairs(Players:GetPlayers()) do
        local Char = Player.Character or Player.CharactedAdded:Wait()
        local GearInPlayer = Player:FindFirstChild("Backpack")
        local GearInCharacter = Char:GetChildren()
        GearInPlayer:ClearAllChildren()
        for _, Tool in pairs(GearInCharacter) do
            if Tool:IsA("Tool") then
                Tool:Destroy()
            end
        end
    end
end


function sortTeams()
	chooseMap()
	loadMap()
	wait(3)
	local Players = game.Players:GetPlayers()
	local players = teams.Lobby:GetPlayers()
	
	local index = math.random(1,#players) -- choose a random index between 1 and the length of the table
	players[index].Team = teams["FirstResponder"] -- set the randomly chosen player to the FirstResponder team
	table.remove(players,index) -- removes them from the pool of players, ensuring no duplicate picking later
	
	
	table.foreach(players,function (_,player) player.Team = teams["Infected"] end)
	teleportPlayers()
	--game.ReplicatedStorage.timer:FireAllClients() (this is a timer for the players)
	print("time")
	local localtimer = roundtime
	while localtimer > 0 do
		wait(1)
		status.Value = localtimer
		localtimer = localtimer - 1
		local kactive = false
		local activepeople = {}
		for _, s in pairs(teams.Infected:GetPlayers()) do
			if s then
				table.insert(activepeople, s)
			end
		end
		for _, k in pairs(teams.FirstResponder:GetPlayers()) do
			if k then
				kactive = true
			end
		end
		if #activepeople < 1 then
			--game.ReplicatedStorage.FirstResponder:FireAllClients() (this is a winners/losers text)
			status.Value = "Never Give Up!" --infected dies, responder alive
			for _, k in pairs(teams.FirstResponder:GetPlayers()) do
				-- k.Cash.Value = k.Cash.Value + 50 (gives cash)
			end
			break
		end
		if not kactive then
			--game.ReplicatedStorage.Infected:FireAllClients() (this is a winners/losers text)
			status.Value = "Without First Responders the Virus Wins!" -- responder dies, infected left
			for _, s in pairs(teams.Infected:GetPlayers()) do
				--s.Cash.Value = s.Cash.Value + 20 (gives cash)
			end
			break
		end
		if localtimer == 0 then
			status.Value = "Good Teamwork Everyone!" -- resonder and survivor alive
			--game.ReplicatedStorage.Infected:FireAllClients() (this is a winners/losers text)
			for _, s in pairs(teams.Infected:GetPlayers()) do
				--s.Cash.Value = s.Cash.Value + 20 (gives cash)
			end
		end
	end
			
	-- Back To Lobby --
	
	roundtime = 0
	wait(3)
	teleportBack()
	removeItems()
	deleteMap()
	table.foreach(Players,function (_,Player) Player.Team = teams["Lobby"] end)
end

-- MAIN FUNCTION

while true do
	local skip = false
	status.Value = ""
	print("Inter")
	--game.ReplicatedStorage.EnoughPlayers:FireAllClients() (gets rid of need more players text)
	--game.ReplicatedStorage.Inter:FireAllClients() (Intermission text) --- changing for >=1 to test
	while inter > 0 do
		local ps = game.Players:GetPlayers()
		if #ps >= 2 then
			status.Value = "Intermission For: "..inter
			inter = inter - 1
			wait(1)
		else
			while #ps < 2 do
				status.Value = "2 or More Players To Play"
				ps = game.Players:GetPlayers()
				--game.ReplicatedStorage.NeedMorePlayers:FireAllClients() (tells the player they need more players to play)
				print("Need More")
				wait(1)
			end
			skip = true
			inter = 0
		end
	end
	wait(1)
	if skip == false then
		sortTeams()
		print("round")
		while roundtime > 0 do
			wait(1)
		end
	end
	print("not enough")
	skip = false
	inter = 20
	roundtime = 360
end

Try to wrap it in a pcall function?

1 Like

I’m super novice… I don’t even know what that means. :slight_smile:
I will look it up though. Thanks!

1 Like

How are you novice… your using tables and inserting and removing them in certain places thats not novice at all…

1 Like

I go this round script from a post. All I did was add the “removeItems” function, which I also got from a post :smiley: In other words, just cobbling things together until it works. And it actually does! This seems to be the last hurdle! :smiley:

Wow, well your not gonna learn from doing that if you are serious about developing I suggest you start learning! Best of luck friend! :call_me_hand: :call_me_hand: :call_me_hand:

1 Like

Yes, I know. I’m not really aiming to be a programmer (at least not right away). For now, I seem to be able to resolve the issues, either with some help, or on my own. For my professional development, I hire teams of artist and programmers (this is my personal hobby stuff). :slight_smile:

1 Like

Oh then rock ON DUDE YOUR AWESOME!!!

1 Like

Ha ha. Thanks. Get 'r done :stuck_out_tongue:

YEEEEEEE BRUDDDDDDAAAAAAAAaAAAAAA

1 Like

Fixed it!

  1. removed the following line from the end of the sortTeams function:
    table.foreach(Players,function (_,Player) Player.Team = teams[“Lobby”] end)

  2. placed the removed line and "local Players = … " at the start of the main function as follows:

    while true do
    	local skip = false
    	status.Value = ""
    	print("Inter")
    	while inter > 0 do
    
    		local Players = game.Players:GetPlayers()
    	    table.foreach(Players,function (_,Player) Player.Team = teams["Lobby"] end)
    		
            local ps = game.Players:GetPlayers()
    		if #ps >= 2 then
            -- etc, etc.
    
2 Likes

Ay thats freakin awesomeee :call_me_hand: :call_me_hand: :call_me_hand:

2 Likes