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

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

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

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)

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