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:

    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

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:

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
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)

--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
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