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!
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)