Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I hide a team without members?

Asked by
Time_URSS 146
5 years ago

Greetings, I want a team without members to not show up, but I don't know how to do it. How could I make it?

0
destroy the team, when someone would normally join, remake it User#20388 0 — 5y

1 answer

Log in to vote
2
Answered by
mattscy 3725 Moderation Voter Community Moderator
5 years ago

You can use a combination of PlayerRemoved and a custom function for when a player is added to solve this. When the game runs, you can record all the teams in a table, and set their parent to nil (removing them from the leaderboard):

local Teams = game:GetService("Teams")
RecordedTeams = {}

for _,Team in pairs(Teams:GetChildren()) do
    RecordedTeams[Team.Name] = Team
    Team.Parent = nil
end 

This will store the team under the name of the team in the RecordedTeams dictionary, and then get rid of the team from the leaderboard (by removing it from the Teams service).

Then, when a new player joins a team, you can put the team back inside of the Teams service, making it appear again:

local function SetTeam(Player,TeamName)
    if RecordedTeams[TeamName] then
        RecordedTeams[TeamName].Parent = Teams
        Player.Team = RecordedTeams[TeamName]
    end
end

This will re-insert the team if it isn't already there, while also setting the given player's team to be that team.

Finally, you want to remove the team when the last player in it leaves. This is where the PlayerRemoved function comes in.

for _,Team in pairs(RecordedTeams) do
    Team.PlayerRemoved:Connect(function()
        if #Team:GetPlayers() <= 0 then --if the player leaving was the last player in the team
            Team.Parent = nil
        end
    end)
end

Hope this helps!

0
Great, thank you. Time_URSS 146 — 5y
Ad

Answer this question