Players left counter

I’m making a hunger game type of game mode and there will be a player counter at the top.
What I’m confused about is how to get the amount of players ALIVE left or just in workspace. Once they have been eliminated, they can only spectate and can’t rejoin. Do I use tables?

Summary: How do I make a players left in workspace counting gui?

edit: I don’t do this type of stuff so call me a noob I don’t know but give me some help please :sweat_smile:

1 Like

Yeah. Use Plr.PlayerRemoved function to add the player to a table. If they rejoin and are in the table then they spectate.

1 Like
--when game starts:Connect function()
teleport players
for each _, i do
if i.parent == "Player" then
playerCounter += 1
end
1 Like

You would have a script that looks somewhat like this however you would use what fungi said so that it subtracts a player if one leaves the game.

1 Like

Depends on your system. For starters, how exactly are you determining which players are alive or currently in the round? If you have a way to spawn them in then ideally you should have a way to track that they’re in the game. Are you using teams for this, or tracking them in some other way, or…?

2 Likes

Uh it goes like this:
I have LoadCharacter set to false and when player click play, they spawn.
The screen gui has resetonspawn false so when they die, instead of play, it’s spectate

and the game itself:
the players spawn on a platform on a giant map
When the platform disappears, the players fall to a spot and start collecting items (fortnite ref?)
When the timer is up, players can attack each other
When a player dies, their character is removed until the next round from workspace bec of LoadCharacter
When there’s nobody left or the timer runs out, the people/person wins.

Also I made it so after a few seconds, it changes the screengui but the people who were originally in can spawn late but people who join later get the spectate one
(because I can only use text button to change GUIs not scripts

You can use tables, but Teams are more efficient for round-based games as it allows you to differentiate spectators from people who haven’t pressed play yet, for example.

An example of a round handler being:

local teams = game:GetService("Teams")
local playersLeft = 0

local function RoundStart()
    for _,v in pairs(teams.Spectators:GetPlayers()) do
        -- adds spectators to playing
        v.Team = teams.Playing
        v:LoadCharacter() 
        playersLeft += 1 

        local char = v.Character or v.CharacterAdded:wait()
        local hum = char:WaitForChild("Humanoid")

        hum.Died:Connect(function() -- removes players who die
            v.Teams = teams.Spectators
            playersLeft -= 1

            if playersLeft <= 1 then
                -- make an event for the round winner and change their team back
            end
        end)    
    end
end

game.Players.PlayerRemoving:Connect(function(plr) -- removes players who disconnect
    if plr.Team == teams.Playing then
        plr.Team = teams.Spectators
        playersLeft -= 1
    end

    if playersLeft <= 1 then
        -- make an event for the round winner and change their team back
    end
end)

-- reference the RoundStart function whenever you want the round to start

Don’t forget to add the Teams in explorer:
image

Then when you detect when a player presses Play, you need to change their team to Spectators. You’re going to need to use RemoteEvents so the team change can be done server-side:

-- LocalScript

local event = game:GetService("ReplicatedStorage"):WaitForChild("PlayEvent")
local button = -- reference the play button here

button.MouseButton1Click:Connect(function()
    event:FireServer()
end)
-- Server RemoteEvent handler

local teams = game:GetService("Teams")
local event = game:GetService("ReplicatedStorage"):WaitForChild("PlayEvent")

local function ButtonPressed(player)
    if player.Team == teams.Unassigned then
        player.Team = teams.Spectators
    end
end

event.OnServerEvent:Connect(ButtonPressed)

image

Hope this helped!

3 Likes

remotefunction in ReplicatedStorage called “getAlivePlayers”
and remoteevent in ReplicatedStorage called JoinRound

server script in ServerScriptService

local PlayersAlive = {}
local ps = game:GetService("Players")
local rs = game:GetService("ReplicatedStorage")
local RoundJoinEvent = rs:WaitForChild("JoinRound",5) 
local getAlivePlayers= rs:WaitForChild("getAlivePlayers",5) 
function CreateTimer(d)
 local t2 = os.time()+d
 repeat wait() until os.time() >= t2
 return true
end

local c = RoundJoinEvent.OnServerEvent:Connect(function(p)
 if not PlayersAlive[p.Name] then
  PlayersAlive[p.Name] = true
 end
end)
repeat wait() until CreateTimer(30) == true -- delay so players can join
c:Disconnect() -- players can no longer join
getAlivePlayers.OnServerInvoke = function()
 return #PlayersAlive
end)

-- When players die, remove them from table using PlayersAlive[p.Name] = nil
1 Like