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

How can I make teams delete if no one is on said team?

Asked by
s_21 74
6 years ago

So I have this script, Where if you are in a certain group you can join the team, Using a GUI, but I was wondering, How can I make it if when no one is on said team, So they left the game or changed teams the team deletes? This is my team change script below.

local plr = game.Players.LocalPlayer
local group = 000000

script.Parent.MouseButton1Down:Connect(function()
    if plr:IsInGroup(group) then
        local newTeam = Instance.new("Team",game.Teams);
        plr.Team = newTeam;
    end
end)

Thanks!

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
6 years ago
Edited 6 years ago

You can setup a PlayerAdded event to track when the Team property is changed using the GetPropertyChangedSignal function.

This function returns an RbxScriptSignal object, which you can then use the Connect function on.

Compare their last team's number of players using #team:GetPlayers(). If it is 0, then you can destroy the team :)

game.Players.PlayerAdded:Connect(function(plr)
    local team = plr.Team --last team
    plr:GetPropertyChangedSignal("Team"):Connect(function()
        --if team is different and oldteam has no players
        if plr.Team ~= team and #team:GetPlayers() < 1 then
            team:Destroy()
        end
    end)
end)

As for when the players leave, you can do the same thing inside of a PlayerRemoving event.

game.Players.PlayerRemoving:Connect(function(plr)
    local team = plr.Team
    if #team:GetPlayers() < 1 then
        team:Destroy()
    end
end)
Ad

Answer this question