Code works in the command bar but not in the script it's self

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!

1 Like

This is odd. I tested it myself and it is working in a script. if there are any errors in the output be sure to send them as it might help

1 Like

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

There are no errors in the output. It’s odd that it’s working for you but not me.

I don’t have to keep printing it. I’m just trying to get the code that detects when the team is empty then I was going to make it stop the loop later.

Let me try the code then get back to you.

Again I apologize if I’m not sounding professional enough. I’m new to this.

Thanks for the help!

EDIT: I tried the code and I got this error for line 2:

ServerScriptService.PlayerCounter:24: attempt to call a RBXScriptSignal value

I apologize if this is an obvious fix. I don’t work with teams very often.

is it possible for you to get a video of you running the code?

1 Like

Yes. I’ll make a video and send it tomorrow morning. Thanks again for all your help!

Can you please send the code snippet of where this error happens?

EDIT: My mistake, I made an error on my previous post, try the code snippet I’ve posted before again.

1 Like

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.

Can you send the code snippet of where you handle that?

1 Like

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)

Sorry for all the trouble.

Server Script or Local Script
as i believe you can only change team on the server

1 Like

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.

now you know how not to do it now, so no harm done!

1 Like