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

Script creates more than one team when called again?

So when a player clicks on "Create Team" button, the script creates a new team, then is parented to the Teams Service; as well as a desired name and Color for the team. The first team that is created works, but after I try to create another test Team with another color; more than 2 are created with the same name AND color. I've used debounces to see if it would fix but it has not.

Here is the script with excluded code that isn't necessary:

local globalTeams = {}
local teamName = ""
local teamColor;

local CreateTeamGui = script.Parent
local CreateTeamActivated = false

debounce1 = false
function CreateNewTeam(newTeamName) --This creates the new team. The problem may be in here
	if debounce1 == true then return end
	
	debounce1 = true
	local newTeam = Instance.new("Team");
	newTeam.Parent = game:GetService("Teams")
	newTeam.Name = newTeamName
	newTeam.TeamColor = gotColor
	
	debounce1 = false
end

debounce2 = false
function nameInput(name)
	if debounce2 == true then return end
	debounce2 = true
	if CreateTeamActivated == true then
		teamName = name
		CreateTeamGui.CreateButton.MouseButton1Down:connect(function() CreateNewTeam(teamName) end) --When "CreateButton"is clicked, that function is fired
	end
	debounce2 = false
end

script.Parent.Parent.GuiHandler.ForCreateTeam.OnServerEvent:connect(function() --THIS SCRIPT SHOULD START WHEN THIS EVENT IS FIRED FROM A LOCAL SCRIPT
	if script.Parent.Parent.CreateTeamActive == false then --Checks a value outside of the script
		return
	else
		CreateTeamActivated = true
	end
end)


CreateTeamGui.TeamNameTextBox.FocusLost:connect(function() --This function handles the part where a player inputs their desired name into a textbox for the team.
	nameInput(CreateTeamGui.TeamNameTextBox.Text) --This may also be the problem. If not its in the function "nameInput"!
end)



1 answer

✓ Accepted answer
function nameInput(name)
    if CreateTeamActivated == true then
        teamName = name
        CreateTeamGui.CreateButton.MouseButton1Down:connect(function() CreateNewTeam(teamName) end) --When "CreateButton"is clicked, that function is fired
    end
end

In this function, you create another connection every time it is called. The connection should be created only once.

local teamName=""
local CreateTeamGui=script.Parent

CreateTeamGui.TeamNameTextBox.FocusLost:connect(function()
	teamName=CreateTeamGui.TeamNameTextBox.Text
end)

CreateTeamGui.CreateButton.MouseButton1Down:connect(function()
	local newTeam=Instance.new("Team")
	newTeam.Parent=game:GetService("Teams")
	newTeam.Name=teamName
	newTeam.TeamColor=gotColor
end)

1 comment

0

Thanks! Got it to work.

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