How can I return the last player`s alive name?

Hello! I have a minigame and I want it to stop if all the people died. So I made 2 teams: Game,Lobby. I made it so when you die u get automaticaly transferred to the lobby team but I dont know how I can make my script stop when there s no one in the game team. Any help?

2 Likes

The Team object has a method GetPlayers and an event PlayerRemoved, so if you listen for when players are removed from the team and check the number of players on the team using GetPlayers, you can tell when the last player is removed.

7 Likes

hi, its rly late for me so i cannot really try doing it but can you like maybe write it or at least write how I can measure how many players there are in a team using GetPlayers?

GetPlayers returns a table of all of the players, so just checking its length tells you how many players are on the team, e.g. numplayers = #team:GetPlayers()

1 Like

Well, i’m new at this and i want to ask you this is the correct way to do this?:

for TeamPlayers, v in pairs(game.Teams.TeamBigRobuxFlam:GetChildren()) do
if TeamPlayers == 0 then
 --Function here
 end
end)

I know that something here is wrong but, what is it.

2 Likes
local inGameTeam = -- however you want to get a reference to it
inGameTeam.PlayerRemoved:Connect(function()
    if #inGameTeam:GetPlayers() == 0 then
        -- End the minigame
    end
end)
1 Like

I create a part in my NPCs saying what team they are on. Then I just Find First Child.

You can do:

Players = game:GetService("Players")

local RedTeamCount = 0

for i, player in pairs(Players:GetPlayers()) do
    local part = workspace:FindFirstChild("RedTeam")
	if part then
		RedTeamCount = RedTeamCount + 1
	end
end

Then if the count is zero, end game or what ever.

I find this helps with targeting too. But Maybe it’s the hard way. I don’t know the team stuff as much. Basically I make them wear a shirt that says what team they are on. I find it easier than having some guy remember who is on what team and have him remove current players based on their name. Just more organic to me. I do know that arrays and functions would probably cut down on actions it needs to perform. It will be less CPU intensive but it makes it easier for me for numerous things that want to know what team they are in.

1 Like

Hi I did this but it doesnt work. My minigame ends when the winner value is not null so I tried to change it to the last player`s name so it shows who won. But it gives me errors and it doesnt work…

local inGameTeam = game.Teams.Game
inGameTeam.PlayerRemoved:Connect(function()
    if #inGameTeam:GetPlayers() == 1 then
	    local x = inGameTeam:GetPlayers()
        game.ReplicatedStorage.vals.Winner.Value = x[1]
    end
end)

Players aren’t stored in the team hence why it isn’t finding anyone, it should look more like this:

local playersInTeam = 0

for i, v in pairs(game.Players:GetChildren()) do
    if v.Team == game.Teams.TeamBigRobuxFlam then
        playersInTeam = playersInTeam + 1
    end
end

if playersInTeam == 0 then
    --Function here
end
1 Like

You should use GetPlayers rather than GetChildren in this situation.

As for the question, it’s unclear what your specific goal is. Other users have suggested using Team:GetPlayers() and Team.PlayerRemoving which is about all we can give with your post.

What error(s) are you receiving in the above post? Additionally you should make sure you use WaitForChild (for the team variable) and GetService. (For services including ReplicatedStorage). This will make sure that instances/services are reliably found.

1 Like

Look, I want to make it so the game stops when there is only 1 player alive and get that player`s name to make him the Winner. Can u help me with that?

Alright, then read the next paragraph where I asked for the information I need.

1 Like

U can use both, but i just want to get the last player`s alive name and change a replicated storage value to that.

Honestly completely forgot about GetPlayers() haven’t used it in a long while

1 Like

won’t GetPlayers () only return an array of the players in the server?
Or does it return only alive players , as the OP wants to see whether players exist in a team in context , relating to the scenario, where dead players don’t have to be counted

1 Like

From what I can tell your code should do that, however I need to have the errors you are having so we can diagnose the problems.

1 Like

It returns all of them. You can see for yourself here: Players | Documentation - Roblox Creator Hub

1 Like

It gives me no errors but it just doesnt work.

The reason I was asking was because you had said “But it gives me errors and it doesnt work…”

Try debugging the code you have. When a player leaves print out the number of players so you know if your code is running. Additionally, what if the winner leaves and there are no players? Could this be what’s causing problems?

1 Like

I just used bad words to describe what was happening. I`ll try and come back here.