#round wont end when all the players die

1 messages · Page 1 of 1 (latest)

dapper orbit
#
local intermission = 20

local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

local inRound = replicatedStorage:FindFirstChild("inRound")
local status = replicatedStorage:FindFirstChild("status")

-- Helper function to remove all tools from all players
local function removeAllPlayerTools()
    for i, plr in players:GetPlayers() do
        -- Remove tools from Backpack
        local backpack = plr:FindFirstChild("Backpack")
        if backpack then
            for j, item in backpack:GetChildren() do
                if item:IsA("Tool") then
                    item:Destroy()
                end
            end
        end
        -- Remove tools from Character
        local char = plr.Character
        if char then
            for j, item in char:GetChildren() do
                if item:IsA("Tool") then
                    item:Destroy()
                end
            end
        end
    end
end

-- Teleport players when round state changes
inRound.Changed:Connect(function ()
    if inRound.Value == true then
        -- Teleport all players to the map spawner
        for i, plr in players:GetPlayers() do
            local char = plr.Character
            if char then
                local humanRoot = char:FindFirstChild("HumanoidRootPart")
                if humanRoot then
                    if workspace:FindFirstChild("Mapspawner") then
                        humanRoot.CFrame = workspace.Mapspawner.CFrame
                    end
                end
            end
        end
    else
        
#
for i, plr in players:GetPlayers() do
            local char = plr.Character
            if char then
                local humanRoot = char:FindFirstChild("HumanoidRootPart")
                if humanRoot then
                    if workspace:FindFirstChild("Lobbyspawner") then
                        humanRoot.CFrame = workspace.Lobbyspawner.CFrame
                    end
                end
            end
        end
        -- Remove all tools from all players when round ends
        removeAllPlayerTools()
    end
end)

-- Helper function to check if all players are dead or in lobby (not alive in the round)
local function allPlayersDeadOrInLobby()
    local lobbySpawner = workspace:FindFirstChild("Lobbyspawner")
    if not lobbySpawner then
        return false
    end
    local lobbyPos = lobbySpawner.Position
    local threshold = 20 -- slightly increased for safety

    for i, plr in players:GetPlayers() do
        local char = plr.Character
        if char then
            local humanoid = char:FindFirstChild("Humanoid")
            local humanRoot = char:FindFirstChild("HumanoidRootPart")
            if humanoid and humanRoot then
                local dist = (humanRoot.Position - lobbyPos).Magnitude
                if humanoid.Health > 0 and dist > threshold then
                    -- Player is alive and not in lobby, round should continue
                    -- print("Player " .. plr.Name .. " is alive and not in lobby (distance: " .. tostring(dist) .. ")")
                    return false
                end
            end
        end
        -- If no character or no humanoid/humanoidroot, treat as dead/in lobby
    end
    -- All players are dead or in lobby
    return true
end
#
local function roundsystem()
    while true do
        inRound.Value = false
        for i = intermission, 0, -1 do
            status.Value = "Game will start in " .. i .. " seconds"
            task.wait(1)
        end
        inRound.Value = true
        status.Value = "Round in progress! Find the trophy to end the round."
        -- Wait until inRound is set to false by the trophy script OR all players are dead or in lobby
        repeat
            task.wait(1)
            if allPlayersDeadOrInLobby() then
                inRound.Value = false
                status.Value = "All players are dead! Restarting round..."
                -- Remove all tools from all players when round ends due to all dead
                removeAllPlayerTools()
                break
            end
        until inRound.Value == false
        status.Value = "Round ended! Next round soon..."
        -- Remove all tools from all players when round ends normally (redundant, but ensures cleanup)
        removeAllPlayerTools()
        task.wait(2)
    end
end

task.spawn(roundsystem)
#

i had to cut the code in 3 cause discord wont allow me to send 😭

lunar lagoon
#

I would keep a table of players who participated inside the round and check if they leave or die

#

Instead of using allPlayersDeadOrInLobby

#

It would make it easier to track and manage players

dapper orbit
#

huh

#

that might work

#

let me test that

dapper orbit
#

i tried fixing it for 3 hours

#

thank you so much dude 🙏

lunar lagoon
#

Yeah