Hello. So I’m working on a script that detects when a team is empty and prints “You Lost”. The code isn’t working in the script it’s self but works fine when I copy and paste it into the command bar.
Here is my code:
local SurvivorTeam = game:GetService(“Teams”):WaitForChild(“Survivors”)
local IsEmpty = nil
while wait(1) do
IsEmpty = #SurvivorTeam:GetPlayers() == 0
if IsEmpty == true then
print("You Lost")
elseif IsEmpty == false then
continue
end
end
I apologize if this isn’t very clear or if I’m doing this wrong. This is my first post. Thanks!
Do you need to keep printing “You Lost” after the team is empty? If not, you can do this more efficiently by using PlayerRemoved.
local SurvivorTeam = game:GetService("Teams"):WaitForChild("Survivors")
local connection; connection = SurvivorTeam.PlayerRemoved:Connect(function(player)
if (#SurvivorTeam:GetPlayers() == 0) the
print("You Lost")
-- optional: disconnect the connection if you are not listening anymore after hitting 0
connection:Disconnect()
end
end)
Hello. I tried your edited code and made it so when the player dies they get put into the “Prisoners” team But it still didn’t work. I’m very sorry for all the trouble.
Here you go.
This is in StarterCharacterScrips inside StarterPlayer by the way
script.Parent.Humanoid.Died:Connect(function()
local Player = game.Players:GetPlayerFromCharacter(script.Parent)
Player.Team = game.Teams.Prisoners
end)
Yes I realized that a few minuets ago that the script that changes the player to the “Prisoners” team is a local script and the script that detects when the team is empty is a server script…So I changed the team change script to a server script and now it works perfectly…
So it turns out that I’m an idiot. lol
Thank you both VERY much for the help. And I apologize for my stupidity.