If there’s a Team called “Headquarters” then the player will be get team if there is no team called “Headquarters” the game will create a new Team using Instance.new()
and if the player leave the game and there is no players in the team the team will be get deleted how do I do that?
Can someone help me please I need it right now and IDK how to do it.
Its Fairly simple. Try this:
game.Players.PlayerAdded:Connect(function(plr) -- When a player joins
if game:GetService("Teams"):FindFirstChild("Headquarters") then --Checks if headquarters team exist
plr.Team = game:GetService("Teams").Headquarters --Player Joins the team
print(plr.Name.." joined Headquarters")
else --If NOT
local Headquarters = Instance.new("Team",game:GetService("Teams")) --Creates the team
Headquarters.Name = "Headquarters"
Headquarters.TeamColor = BrickColor.Yellow() --Your team color [OPTIONAL]
plr.Team = Headquarters --Player joins the team
print("Team Created")
print(plr.Name.." joined Headquarters")
end
end)
game.Players.PlayerRemoving:Connect(function(plr) --When a player leaves
if game:GetService("Teams"):FindFirstChild("Headquarters") then --If Headquarters team exist
if #game:GetService("Teams"):FindFirstChild("Headquarters"):GetChildren() == 0 then --Checking players in team
game:GetService("Teams"):FindFirstChild("Headquarters"):Destroy() --Destroys the team
print("Team Deleted")
end
end
end)
But I’m checking if the player leaves the team not player leaves a game
Try the PlayerRemoved event. It is an event of the team instance. It is fired every time a player has left that team.
game:getService("Teams").PlayerRemoved:Connect()
?
It would be
game.Teams.Headquarters.PlayerRemoved:Connect(function(plr)
-- Before adding this event listening, make sure the team exists.
end)
Like this?
local Teams = game:GetService('Teams')
if not Teams:FindFirstChild('Headquarters') then --//Checking if the team exists
local newTeam = Instance.new('Team')
newTeam.TeamColor = BrickColor.new('Toothpaste')
newTeam.Name = 'Headquarters'
newTeam.Parent = Teams --//Making the team
end
Teams:FindFirstChild('Headquarters').PlayerRemoved:Connect(function(plr)
-- Before adding this event listening, make sure the team exists.
end)
1 Like
That should work. If you ever create the team again after it is destroyed in the same server, you will need to hook up the event listener again.
How do I do that? Can you please tell me?
I was thinking about this
Teams.ChildAdded:Connect(function(team) --//Called when the team gets added
if team.Name == 'Headquarters' then
team.PlayerRemoved:Connect(function() --//Called when a player leaves
if table.getn(team:GetPlayers()) == 0 then --//Checking how many players are on the team, if it is 0 then we will proceed
team:Destroy() --//Removing the team
end
end)
end
end)
You said when the player leaves the game. But ok, you could do this:
game:GetService("Teams").ChildAdded:Connect(function(team)
wait(1) --delay for load
print(team.Name.." has been added")
if team.Name == "Headquarters" then --Checking if the team is Headquarters
team.PlayerRemoved:Connect(function()
if #team:GetPlayers() == 0 then --Checks if theres no player
team:Destroy() --Deletes the team
print("Team Deleted")
end
end)
end
end)
Read again
It will be good right?
local Teams = game:GetService('Teams')
if not Teams:FindFirstChild('Headquarters') then --//Checking if the team exists
local newTeam = Instance.new('Team')
newTeam.TeamColor = BrickColor.new('Toothpaste')
newTeam.Name = 'Headquarters'
newTeam.Parent = Teams --//Making the team
end
Teams.ChildAdded:Connect(function(team)
wait(1) --delay for load
print(team.Name.." has been added")
if team.Name == "Headquarters" then --Checking if the team is Headquarters
team.PlayerRemoved:Connect(function()
if #team:GetPlayers() == 0 then --Checks if theres no player
team:Destroy() --Deletes the team
print("Team Deleted")
end
end)
end
end)