Help me to organize my round system

I want my code base to be cleaner.

local ServerStorage = game:GetService("ServerStorage")
local maps = ServerStorage.Maps

local Workspace = game:GetService("Workspace")
local lobby = Workspace.Lobby
local lobbySpawnLocation = lobby.SpawnLocation

local Players = game:GetService("Players")

local SetMessageTimer = require(script.SetMessageTimer)
local GetRandomChild = require(script.GetRandomChild)
local TeleportPlayerTo = require(script.TeleportPlayerTo)

while true do
	SetMessageTimer(function(timeLeft: number) return `Intermission ends in {timeLeft}` end, 5)
	
	local newMap = GetRandomChild(maps):Clone()
	local newMapSpawnLocations = newMap.SpawnLocations
	newMap.Parent = Workspace
	
	for _, player in Players:GetPlayers() do
		TeleportPlayerTo(player, GetRandomChild(newMapSpawnLocations).CFrame)
	end
	
	local AlivePlayers = Players:GetPlayers()
	for playerIndex, player in Players:GetPlayers() do
		local character = player.Character
		local humanoid = character:FindFirstChildOfClass("Humanoid")
		if humanoid ~= nil then
			humanoid.Died:Once(function()
				AlivePlayers[table.find(AlivePlayers, player)] = nil
			end)
		else
			AlivePlayers[table.find(AlivePlayers, player)] = nil
		end
	end
	
	SetMessageTimer(function(timeLeft: number) return `Round ends in {timeLeft}` end, 10, function()
		return #AlivePlayers == 0
	end)
	
	for _, player in Players:GetPlayers() do
		TeleportPlayerTo(player, lobbySpawnLocation.CFrame)
	end
end
local ServerStorage = game:GetService("ServerStorage")
local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")

local maps = ServerStorage.Maps
local lobbySpawnLocation = Workspace.Lobby.SpawnLocation

local SetMessageTimer = require(script.SetMessageTimer)
local GetRandomChild = require(script.GetRandomChild)
local TeleportPlayerTo = require(script.TeleportPlayerTo)

local function teleportPlayersTo(players, spawnLocations)
    for _, player in ipairs(players) do
        TeleportPlayerTo(player, GetRandomChild(spawnLocations).CFrame)
    end
end

local function trackAlivePlayers(players)
    local alivePlayers = {}

    for _, player in ipairs(players) do
        local humanoid = player.Character and player.Character:FindFirstChildOfClass("Humanoid")
        if humanoid then
            humanoid.Died:Once(function() alivePlayers[player.UserId] = nil end)
            alivePlayers[player.UserId] = player
        end
    end

    return alivePlayers
end

while true do
    SetMessageTimer(function(timeLeft) return `Intermission ends in {timeLeft}` end, 5)

    local newMap = GetRandomChild(maps):Clone()
    newMap.Parent = Workspace
    teleportPlayersTo(Players:GetPlayers(), newMap.SpawnLocations)

    local alivePlayers = trackAlivePlayers(Players:GetPlayers())

    SetMessageTimer(function(timeLeft) return `Round ends in {timeLeft}` end, 10, function()
        return next(alivePlayers) == nil -- Check if alivePlayers is empty
    end)

    teleportPlayersTo(Players:GetPlayers(), {lobbySpawnLocation})
end
1 Like

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