Textbox not creating new team when there is an existing team

Hello, i am creating a simple project where you enter a custom team name into a textbox, and it will create a team with the input of the textbox. However, when there is already a team, and the player enters another team name, it doesn’t add a new team or delete the old team.

I have looked on devForum but I couldn’t really find any useful information, but I think something is wrong with the get players, but I dont really know.

here is my local script inside the textbox:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("ChangeTeamEvent")  -- Ensure the RemoteEvent is here
local txtBox = script.Parent  -- The textbox in the GUI
local player = game.Players.LocalPlayer  -- The local player

txtBox.FocusLost:Connect(function()
	local newTeamName = txtBox.Text  

	if newTeamName then
		remoteEvent:FireServer(newTeamName)
	end
end)

and this is my script inside serverscriptservice:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("ChangeTeamEvent")
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")

remoteEvent.OnServerEvent:Connect(function(player, newTeamName)

	local existingTeam = Teams:FindFirstChild(newTeamName)
	
	if existingTeam then

		local playersInTeam = existingTeam:GetPlayers()

		if #playersInTeam == 0 then
			existingTeam:Destroy()
		end
	end
	
	local newTeam = Instance.new("Team")
	newTeam.Name = newTeamName
	newTeam.Parent = Teams 
	player.Team = newTeam
end)

Sorry if this is easy to fix, I am new to Roblox Studio and I am still trying by best to improve at Roblox.

1 Like

u are checking if the existing team amount is == 0, but that wont happen because the player is in that team, causing ur function to never destroy the team

Hi, thank you for the reply, should i add switch the player’s teams before deleting the team?

u should put #playersInTeam == 1 then

im not expert at using teamservice, but maybe u should only destroy the team if the player left in the team before you leave, is you

also when u create instance.new(“Team”)
make sure to also put the property “AutoAssignable” to false, so new players wont be automatically joined into any of them

Hello, I updated the script, but it is still not creating a new team:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("ChangeTeamEvent")
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")

remoteEvent.OnServerEvent:Connect(function(player, newTeamName)
	
	local existingTeam = Teams:FindFirstChild(newTeamName)

	if existingTeam then
		local playersInTeam = existingTeam:GetPlayers()

		if #playersInTeam == 1 then
			player.Team = nil
			existingTeam:Destroy()
		end
	end

	local newTeam = Instance.new("Team")
	newTeam.Name = newTeamName
	newTeam.Parent = Teams 
	newTeam.AutoAssignable = false  
	player.Team = newTeam  
end)

the reason why that was happening was because the team wasn’t being assigned a brick color. teams are heavily reliant on brick colors.

team-changer

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("ChangeTeamEvent")
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")

remoteEvent.OnServerEvent:Connect(function(player, newTeamName)

	local existingTeam = Teams:FindFirstChild(newTeamName)

	if existingTeam then

		local playersInTeam = existingTeam:GetPlayers()

		if #playersInTeam == 0 then
			existingTeam:Destroy()
		end
	end

	local newTeam = Instance.new("Team")
	newTeam.Name = newTeamName
	newTeam.Parent = Teams 
	newTeam.TeamColor = BrickColor.random()
	player.Team = newTeam
end)

1 Like

Hi, thanks for the response, It worked like I wanted to.

i’m glad i could help :+1:

car