Round based with last 3 winner in order

Hello I am a little developed I know how to create a basic tower system with a winner who is the last alive but I would like to make him wait to have 1 survivor and he tells us the first survivor, second survivor and third survivor in the order of their deaths as on Lab experiment which indicates the last 3 living in order.

I’m not asking for a pre-made script just if you could show me how to do this for me to learn because I don’t know how to do it.

When someone dies, you can add them to a table. When there’s only one player left, that one player will be the winner. The second one will be the one who died last, and the third one will be the one who died second last. The code could be something like this:

local Players = game:GetService("Players")

local deadPlayers
local alivePlayers
local diedConns
local numOfPlayersInRound

local function doSomethingWithLastThree(winner, second, third)

end

local function removePlayerFromRound(player)
    diedConns[player]:Disconnect()
    diedConns[player] = nil
    local indexInAlivePlayers = table.find(alivePlayers, player)
    if indexInAlivePlayers then
        table.remove(alivePlayers, indexInAlivePlayers)
    end
    numOfPlayersInRound -= 1
    if numOfPlayersInRound == 1 then
        local numOfDead = #deadPlayers
        doSomethingWithLastThree(alivePlayers[1], deadPlayers[numOfDead], deadPlayers[numOfDead - 1]
    end
end

-- this should be called when the round starts
local function handlePlayerDeathsDuringRound()
    deadPlayers = {}
    alivePlayers = {}
    diedConns = {}
    
    local players = Players:GetPlayers()
    numOfPlayersInRound = #players
    
    for i, player in ipairs(players) do
        local char = player.Character
        if not char then
            -- The player hasn't spawned yet, so they aren't ready to join a round
            numOfPlayersInRound -= 1
            continue
        end
        local hum = char.Humanoid
        if hum.Health == 0 then
            -- for some reason, the player is already dead.
            table.insert(deadPlayers, player)
            removePlayerFromRound(player)
            continue
        end
        table.insert(alivePlayers, player)
        diedConns[player] = hum.Died:Connect(function()
            table.insert(deadPlayers, player)
            removePlayerFromRound(player)
        end)
    end

    Players.PlayerRemoved:Connect(function()
        removePlayerFromRound(player)
    end
end

You could also use one table to keep track of the death order of the players by moving the players to the end of the table when they die, but I think it may be easier to just use two tables.

2 Likes

Thanks, I didn’t think about using tables to do this.

tables go from first added to last added or in alphabetical order ?

When you add them using table.insert without specifying an index (which is what I did with deadPlayers and alivePlayers) the values are added to the end of the array part of the table so they are sorted from first added to last added.

It is possible to order array values alphabetically or in some other way, but that would be more complicated.

Values in the dictionary part of a table don’t have a clear order. diedConns is a dictionary.