Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How should I remove a team if no one's on it?

I've been pondering this idea, and I'm stumped. I thought a script like this would work fine:

local teams = {"Tourists", "Workers"} -- I have A LOT more teams than this, but I figured I at least want to get the concept down with two teams. I don't really want to use a table since I have about 15 - 20 teams, and putting them into here would be a lot of work.

while wait() do

	for index, player in pairs(game:GetService("Players"):GetPlayers()) do
	
		-- Code, but I'm really stumped on what to put here to check if there are no players inside of a team. Inside this code it would be like team:Destroy(), and I know I have to get the team object.

	end

end

2 comments

0

You could loop through the players, see what team they are on, if anyone is on that team, set a variable to true, then a conditional statement to delete that team if the variable isn't true

0

Are you using the ROBLOX Teams service with instances of type Team for each of your teams? If so, you can use the GetPlayers() function to count how many players are in a team.

1 answer

✓ Accepted answer

Assuming that you're not using the ROBLOX Teams service, which provides a GetPlayers() function to count players...

I'll expand a little bit on the idea suggested by PreciseLogic in his comment.

Before you perform this check, initialise an extra table which will record information about whether there are any players in a team. As part of this algorithm, we'll start by assuming that there aren't any players in any of the teams, so mark all of the entries with false.

local teams_with_players = {}
for i,v in ipairs(teams) do
	teams_with_players[v] = false

Now loop through all of the players and find which team they belong to - I can't do this step for you, because it depends on your implementation. Where do you save which team a Player belongs to?

for index, player in pairs(game:GetService("Players"):GetPlayers()) do
	team = --get which team the player belongs to here
	
	-- Since there is at least one player in `team`, set the marker for that team to `true`
	teams_with_players[team] = true
end

Now teams_with_players will contain entries for each team which describe whether there are any players in them or not, so the ones which don't have any players can be removed.

for team, has_players in pairs(teams_with_players) do
	if not has_players then
		-- Remove 'team'
	end
end

You'll have to figure out how to find which team a player is in and how to remove the teams - these depend on your own design decisions and implementation!

This discussion is preserved as it was. New answers, comments, and votes are closed.