Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do i get the last one player in a team?

Asked by
Techyfied 114
4 years ago

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:

1local team = game:GetService("Teams").Contestants
2local players = team:GetPlayers()
3if #players <= 1 then
4    game.ServerStorage.WinDetector:Clone().Parent = game.Workspace
5end
6if #players == 0 then
7    SendMessage("Couldn't detect a winner.")
8    EndGame()
9end

3 answers

Log in to vote
0
Answered by 4 years ago

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:

1Player:MoveToTeam("Fallen") -- the line where the player changes team
2if #players == 1 then -- the line where you check the amount of players
3 game.ServerStorage.WinDetector:Clone().Parent = game.Workspace
4elseif #players == 0 then
5 SendMessage("Couldn't detect a winner.")
6 EndGame()
7end
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? Techyfied 114 — 4y
0
You already did that, I just thought it didn't work for you because it only checks once Tymberlejk 143 — 4y
Ad
Log in to vote
0
Answered by
Techyfied 114
4 years ago

I have fixed the issue by using Tables. You can get help from this post: https://devforum.roblox.com/t/how-do-i-get-the-last-player-in-a-team/876576/ Thank you all for helping.

Log in to vote
0
Answered by 4 years ago

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)

01--Variables
02 
03TeamName = game.Teams.Fallen --team name
04PlayersOnFallen = 0 --sets the variable to 0
05 
06--main part of the script
07 
08for i,v in pairs(game.Players:GetPlayers()) do --goes through all the players (game.Players)
09    if v.Team == TeamName then --if player is on the fallen team then,
10        PlayersOnFallen = PlayersOnFallen + 1 --adds 1 to the variables
11    end
12end
13 
14print(PlayersOnFallen) --prints out how much player(s) are on the team
15 
16if PlayersOnFallen == 1 then --if there's 1 player on the Fallen team then,
17    game.ServerStorage.WinDetector:Clone().Parent = game.Workspace --clones the win detector
18else
19    print("There still isn't 1 player on the Team Fallen")
20end
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? Techyfied 114 — 4y

Answer this question