Help with constantly check if a team has 0 players

im basically making a team round based system like csgo/valorant

I need help with the part where when the round starts, this script on the serverscriptservice could constantly detect if a team ends up with 0 players to attribute the victory to the opposing team

script below

– Variables

local Teams = game:GetService(“Teams”)
local team1 = Teams.BlueTeam
local team2 = Teams.RedTeam
local Players = game:GetService(“Players”)

local blueTeamPlayers = {}
local redTeamPlayers = {}

function respawnPlayers()
for _, player in ipairs(game.Players:GetPlayers()) do
if player.TeamColor == BrickColor.new(“Bright blue”) or player.TeamColor == BrickColor.new(“Bright red”) then
player:LoadCharacter()
end
end
end

– Start game

local function startGame()

for _, player in pairs(game.Players:GetPlayers()) do
	if player.Team == team1 then
		table.insert(blueTeamPlayers, player)
	elseif player.Team == team2 then
		table.insert(redTeamPlayers, player)
	end
end

if #blueTeamPlayers < 1 or #redTeamPlayers < 1 then
	print("No enough players to start the game.")
	return
end


print("Iniciando o jogo...")

if #Teams.BlueTeam:GetPlayers() == 0 then
	print("Red Team won.")
	respawnPlayers()
elseif #Teams.RedTeam:GetPlayers() == 0 then
	print("Blue Team won.")
	respawnPlayers()
end







-- End
print("Round ended.")

end

game.Players.PlayerAdded:Connect(function(player)

player.Chatted:Connect(function(message)
	
	if player.Name == "DenysMilitar" and message:lower() == "start" then
		startGame()
	end
end)

end)

3 Likes

You can simply run a thread to check:

local Teams = game:GetService('Teams');

local checkBlueTeamPlayers = function()
    while true do
        if #Teams.BlueTeam:GetPlayers() < 1 then
            print('The team has less than 1 player');
        end;

        task.wait(0.2);
    end;
end;

task.spawn(checkBlueTeamPlayers);
1 Like

Bad idea, though you can achieve this with the answer above, I highly suggest you create a modular system where there’s a module script handling the rounds, and when a round ends, another module script handling the leaderboard will be called to clean up.

1 Like

I agree that this is ideal, however, this user seems to be a beginner level scripter and this type of system would be the easiest for them to implement without issues.

1 Like

Hey, I’m not forcing them or anything lol
I’m just giving an opinion and a way to go, if they don’t have enough knowledge on how to make a system like that, then yeah they can use a simple system like this to get the basics.

why’s the thread a bad idea

(and yea im a beginner)

1 Like

its a bad idea because you’re going to be using unnecessary server resources when you could have just have put this if #Teams.BlueTeam:GetPlayers() < 1 then print('The team has less than 1 player'); end; in a player removing function