How do i get the last player in a team?

So, I’m working on a game, where, if you lose. You basically change team. Last one to be in the Contestants team wins. However, I’m having issue on detecting the last person on the Contestant team and award them a win, and teaming them to “Fallen”. I could make a script to count the number of players though.

I found a way by adding a brick to the playzone but i feel like it will easily break the game. That’s why i don’t want to use this method to get the last player of the following team.

Here is what i tried:

local team = game:GetService("Teams").Contestants
local players = team:GetPlayers()
if #players <= 1 then
    game.ServerStorage.WinDetector:Clone().Parent = game.Workspace
if #players == 0 then
    SendMessage("Couldn't detect a winner.")

I want it so an event fires when there is one player in the contestant team, then i will team them “Fallen”, set their health to 0, and add a win to their leaderstats

1 Like

I recommend looking at tables. They would be way better and more efficient to use in this situation.

1 Like

I have tried that method as well. But I couldn’t understand how should i remove the following player from the Table once they touch the LoseBrick.

simply use table.remove() it is on the documentation.

How can i remove a player name’s string from the following table? I have tried but it says I need to remove numberwised player. So how can i find the player name string from the table and remove them?

can you show your code that involves the table?

same ^ I need to understand the table he is talking abt.

oh wait are you talking about getplayers? if so simply use table.remove(team, player.Name) or just remove the player. whichever one is best and works for your code

. if they touch the brick get the player from character and kick the player.

kick players? Why would he want to kick players from the game? That would make things worse. He should use tables in this situatuon.

he said remove player… so he shud prob kick it, shudn’t he?

that is not right at all, he meant remove as in remove them from the list to be the last player standing for his game. Not kick .

I just coded it. Not sure if this is correct.

local PlayersPlaying = {}
for i, v in pairs(game.Players:GetChildren()) do
	table.insert(PlayersPlaying, i, v.Name)

And checking the number of players once the LoseBrick is touched.

	table.remove(PlayersPlaying, hit.Parent.Name)
	if #PlayersPlaying <= 1 then
		SendMessage(tostring(PlayersPlaying[1]).. " has won the game!")

Oh mk, I get it now, He shud use teams in my opinion, What he can do is if the player touches lose brick the player will be in Lose team.

I am using teams. I’m trying to get the last player of the Playing team and announcing them winner.

test it out, we wont know it will work until you test it

yea thats what I am talking abt Check in the team if there is only 1 player left.

that is what he was doing, that was the whole point

Yea, test it if its not working @Techyfied Show us the table we might be able to help you!

you dont need to insert the players name into the table as :GetPlayers() already returns a table of the players for you