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:
1 | local team = game:GetService( "Teams" ).Contestants |
2 | local players = team:GetPlayers() |
3 | if #players < = 1 then |
4 | game.ServerStorage.WinDetector:Clone().Parent = game.Workspace |
5 | end |
6 | if #players = = 0 then |
7 | SendMessage( "Couldn't detect a winner." ) |
8 | EndGame() |
9 | end |
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:
1 | Player:MoveToTeam( "Fallen" ) -- the line where the player changes team |
2 | if #players = = 1 then -- the line where you check the amount of players |
3 | game.ServerStorage.WinDetector:Clone().Parent = game.Workspace |
4 | elseif #players = = 0 then |
5 | SendMessage( "Couldn't detect a winner." ) |
6 | EndGame() |
7 | end |
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.
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 |
03 | TeamName = game.Teams.Fallen --team name |
04 | PlayersOnFallen = 0 --sets the variable to 0 |
05 |
06 | --main part of the script |
07 |
08 | for 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 |
12 | end |
13 |
14 | print (PlayersOnFallen) --prints out how much player(s) are on the team |
15 |
16 | if 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 |
18 | else |
19 | print ( "There still isn't 1 player on the Team Fallen" ) |
20 | end |