Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do i get the last one 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. 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
	end
	if #players == 0 then
		SendMessage("Couldn't detect a winner.")
		EndGame()
	end

3 answers

Your if statements only check once because they're not in a loop. I suggest using the line each time someone gets moved, like so:

Player:MoveToTeam("Fallen") -- the line where the player changes team
if #players == 1 then -- the line where you check the amount of players
 game.ServerStorage.WinDetector:Clone().Parent = game.Workspace
elseif #players == 0 then
 SendMessage("Couldn't detect a winner.")
 EndGame()
end

2 comments

0

That's not what i wanted. How can i detect the player instance that are in the contestant team and is the only player to be in the team?

0

You already did that, I just thought it didn't work for you because it only checks once

For me, I would check all of the players to see if the players are in the Fallen team. I have made a script that checks that. (Mark this answered if this is what you need)

--Variables

TeamName = game.Teams.Fallen --team name
PlayersOnFallen = 0 --sets the variable to 0

--main part of the script

for i,v in pairs(game.Players:GetPlayers()) do --goes through all the players (game.Players)
	if v.Team == TeamName then --if player is on the fallen team then,
		PlayersOnFallen = PlayersOnFallen + 1 --adds 1 to the variables
	end
end

print(PlayersOnFallen) --prints out how much player(s) are on the team

if PlayersOnFallen == 1 then --if there's 1 player on the Fallen team then,
	game.ServerStorage.WinDetector:Clone().Parent = game.Workspace --clones the win detector
else
	print("There still isn't 1 player on the Team Fallen")
end

1 comment

0

I want to get the last one player instance, without using a win detector. game.ServerStorage.WinDetector:Clone().Parent = game.Workspace. Is it not possible?

This discussion is preserved as it was. New answers, comments, and votes are closed.