Old round script

I had decided to revive one of my old games and the round script is total junk, I need help fixing it so I can revive my game.


Note: I did not make this script, my friend did and I edited it.


Know issues:

  • status bar displaying everyone died when a person wins
  • it is very long, please shorten/optimize it if you can

local status = game.ReplicatedStorage.Status
local Maps = game.ReplicatedStorage.Maps:GetChildren()


while true do
	for i = 1,15 do
		status.Value = "Intermission "..15-i
		wait(1.5)
	end

	local rand = math.random(1, #Maps)

	local map = Maps[rand]:Clone()
	map.Parent = workspace

	status.Value = "The obby for this round is "..map.Name
	wait(4)

	local players = game.Players:GetChildren()
	for i = 1,#players do
		if players[i].Character ~= nil then
			local spawnlocation = math.random(1,#workspace.Spawnpoints:GetChildren())
			players[i].Character:MoveTo(workspace.Spawnpoints:GetChildren()[spawnlocation].Position)
			players[i].Character.Parent = workspace.Ingame
		end
	end
	local roundLength = 45
	local canWin = true


	map.Win.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and canWin == true then
			canWin = false
			status.Value = hit.Parent.Name.. " Has won!"

			local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
			plr.leaderstats.Points.Value = plr.leaderstats.Points.Value + 100
			plr.leaderstats.Wins.Value = plr.leaderstats.Wins.Value + 1

		end
	end)

	repeat
		roundLength = roundLength -1
		status.Value = "Time Left: "..roundLength
		wait(1)
	until roundLength == 0 or canWin == false or #workspace.Ingame:GetChildren() == 0
	map:Destroy()
	for i = 1,#players do
		if players[i].Character ~= nil and players[i].Character.Parent == game.Workspace.Ingame then
			players[i].Character:MoveTo(workspace.LobbySpawn.Position)
			players[i].Character.Parent = workspace
	
	if roundLength == 0 then
		status.Value = "Time ran out!"
		wait(0.5)
	end
	if #workspace.Ingame:GetChildren() == 0 then
		status.Value = "Everyone has died!"
				wait(1)
			end
		end
	end	
end

The problem with the status changing to “Everyone has died!” was that the number of players in-game would always be 0. Also, you should’ve checked whether the win part was touched, otherwise it would say everyone died. I noticed that you tried changing the status while looping through the players at the end of the round. You should try changing the status after the loop is complete. Otherwise, it would keep changing the status until the loop is finished and would just take up (in this case) 1.5 seconds for every player the script loops through.

I have made some changes to your script. I’d like to mention that I used a table to retrieve the players in the round, instead of the Ingame folder. It’s what I prefer, but you could change it back to the folder, if you prefer.

I advise that in your future projects, you would use task.wait() instead of wait(). I find it to be a bit faster.

New Round Script
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Status = ReplicatedStorage:WaitForChild("Status")
local Maps = ReplicatedStorage:WaitForChild("Maps")

local PlayersInRound = {}

local function addPlayer(Player:Instance)
	if table.find(PlayersInRound, Player) then return end
	table.insert(PlayersInRound, Player)
end

local function removePlayer(Player:Instance)
	if table.find(PlayersInRound, Player) then
		table.remove(PlayersInRound, table.find(PlayersInRound, Player))
	end
end

local function playerInRound(Player:Instance)
	if table.find(PlayersInRound, Player) then
		return true
	end
end

local function changeStatus(...)
	local Args = {...}
	local newStatus = ""
	for i,v in pairs(Args) do
		if i == 1 then
			newStatus = tostring(v)
		else
			newStatus = newStatus .. " " .. tostring(v)
		end
	end
end

while true do
	for i = 15, 0, -1 do
		task.wait(1.5)
		changeStatus("Intermission:", i)
	end
	
	local mapChildren = Maps:GetChildren()
	local newMap = mapChildren[math.random(1, #mapChildren)]:Clone()
	newMap.Parent = workspace
	
	changeStatus("The obby for this round is:", newMap.Name)
	task.wait(4)

	for _, player in pairs(Players:GetChildren()) do
		if player.Character then
			local SpawnLocation = math.random(1, #workspace.Spawnpoints:GetChildren())
			player.Character:MoveTo(workspace.Spawnpoints:GetChildren()[SpawnLocation].Position)
			player.Character.Parent = workspace.Ingame
			local Humanoid = player.Character:WaitForChild("Humanoid")
			Humanoid.Died:Connect(function()
				removePlayer(player)
			end)
		end
	end
	
	local roundLength = 45
	local winTouched = false

	newMap.Win.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChildOfClass("Humanoid") and winTouched == false then
			winTouched = true
			local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
			if plr then
				changeStatus(hit.Parent.Name, "has won!")
				plr.leaderstats.Points.Value = plr.leaderstats.Points.Value + 100
				plr.leaderstats.Wins.Value = plr.leaderstats.Wins.Value + 1
				PlayersInRound["Winner"] = plr
			else
				winTouched = false
			end
		end
	end)

	repeat
		roundLength -= 1
		changeStatus("Time remaining:", roundLength)
		task.wait(1)
	until roundLength <= 0 or winTouched == true or #PlayersInRound == 0
	newMap:Destroy()
	for _, player in pairs(Players:GetChildren()) do
		if playerInRound(player) then
			local Character = player.Character
			if Character then
				Character:MoveTo(workspace.LobbySpawn.Position)
			end
		end
	end
	if roundLength == 0 and winTouched == false then
		changeStatus("Everyone ran out of time!")
		task.wait(2)
	elseif PlayersInRound["Winner"] == nil and winTouched == false then
		changeStatus("Everyone has died!")
		task.wait(2)
	end
end

Feel free to edit the script according to your own preferences.