-- Script: ServerScriptService/LobbyServer, RunContext = Server
-- Use a fresh practice place. Creates pads near (0, 0, 0) and (80, 0, 0).
-- Put the companion ModuleScript LobbyRound beside this Script.
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local LobbyRound = require(script.Parent:WaitForChild("LobbyRound"))
assert(not Workspace:FindFirstChild("ObbyLobbyPractice"), "Remove the duplicate lobby Script before retrying")
local world = Instance.new("Folder")
world.Name = "ObbyLobbyPractice"
world.Parent = Workspace
local function pad(className, name, position, size, color)
    local part = Instance.new(className)
    part.Name, part.Position, part.Size = name, position, size
    part.Anchored = true
    part.Color = color
    part.Parent = world
    return part
end
local lobby = pad("SpawnLocation", "LobbySpawn", Vector3.new(0, 1, 0),
    Vector3.new(40, 2, 40), Color3.fromRGB(70, 150, 160))
lobby.Neutral = true
lobby.AllowTeamChangeOnTouch = false
local arena = pad("Part", "ArenaFloor", Vector3.new(80, 1, 0),
    Vector3.new(40, 2, 40), Color3.fromRGB(210, 140, 65))
local function register(player)
    player.RespawnLocation = lobby
end
Players.PlayerAdded:Connect(register)
for _, player in ipairs(Players:GetPlayers()) do register(player) end
local function livingCharacter(player)
    local character = player.Character
    if not character or not character.Parent then return nil end
    local humanoid = character:FindFirstChildOfClass("Humanoid")
    if not humanoid or humanoid.Health <= 0 or not character:FindFirstChild("HumanoidRootPart") then return nil end
    return character
end
local function move(character, floor, slot, count)
    -- Distribute pivots in a grid over the 40-stud pad, with a margin.
    local columns = math.ceil(math.sqrt(count))
    local rows = math.ceil(count / columns)
    local x = ((slot - 1) % columns + 0.5) * (32 / columns) - 16
    local z = (math.floor((slot - 1) / columns) + 0.5) * (32 / rows) - 16
    character:PivotTo(floor.CFrame * CFrame.new(x, 5, z))
    local root = character:FindFirstChild("HumanoidRootPart")
    if root then
        root.AssemblyLinearVelocity = Vector3.zero
        root.AssemblyAngularVelocity = Vector3.zero
    end
end
local controller = LobbyRound.new(2, 5, 30)
local roundCharacters = {}
while true do
    local readyIds, readyCharacters = {}, {}
    for _, player in ipairs(Players:GetPlayers()) do
        local character = livingCharacter(player)
        if character then
            table.insert(readyIds, player.UserId)
            readyCharacters[player.UserId] = character
        end
    end
    local update = controller:step(os.clock(), readyIds)
    if update.startIds then
        roundCharacters = {}
        for slot, id in ipairs(update.startIds) do
            local character = readyCharacters[id]
            roundCharacters[id] = character
            move(character, arena, slot, #update.startIds)
        end
    elseif update.returnIds then
        for slot, id in ipairs(update.returnIds) do
            local player = Players:GetPlayerByUserId(id)
            local character = player and livingCharacter(player)
            -- Never move a replacement character or a late joiner as an old participant.
            if character and character == roundCharacters[id] then
                move(character, lobby, slot, #update.returnIds)
            end
        end
        roundCharacters = {}
    end
    world:SetAttribute("Phase", update.phase)
    world:SetAttribute("SecondsRemaining", update.remaining)
    task.wait(0.25)
end
