Cloning Multiplies When Function Is Ran Twice

(I am very new to the devforums so please forgive me for any mistakes)

I am having an issue with cloning a GUI Element into the game. The problem is that every time the function runs, it multiplies the next time the function runs. I have tried to re-arrange the script in certain ways and have looked to see if others had the same problem. While I did find some info it still did not resolve my issue. Any help would be greatly appreciated. Thanks.

local TXTbox = script.Parent
local Base = TXTbox.Parent
local ChatLogBase = Base.ChatLogBase
local ChatLogs = ChatLogBase.ChatLogs

local ServerConnect = game.ReplicatedStorage.ServerConnect

local RepStorage = game:GetService("ReplicatedStorage")
local MessageTemplate = RepStorage.Message

local UIS = game:GetService("UserInputService")



TXTbox.FocusLost:Connect(function()
	UIS.InputBegan:Connect(function(input, gameProcessedEvent)
		if input.UserInputType == Enum.UserInputType.Keyboard then
			if input.KeyCode == Enum.KeyCode.Return then
				print("Client Has Entered There Message")
				ServerConnect:FireServer()
				
				local MsgTempClone = MessageTemplate:Clone()
				MsgTempClone.Parent = ChatLogs
				MsgTempClone.Text = TXTbox.Text
				TXTbox.Text = ""
			end
		end
	end)
end)
1 Like

Here is a video better explaining my problem.

1 Like

Check to see if it exists before cloning it:

if not ChatLogs:FindFirstChild("MessageTemplate") then


local TXTbox = script.Parent
local Base = TXTbox.Parent
local ChatLogBase = Base.ChatLogBase
local ChatLogs = ChatLogBase.ChatLogs

local ServerConnect = game.ReplicatedStorage.ServerConnect

local RepStorage = game:GetService("ReplicatedStorage")
local MessageTemplate = RepStorage.Message

local UIS = game:GetService("UserInputService")



TXTbox.FocusLost:Connect(function()
	UIS.InputBegan:Connect(function(input, gameProcessedEvent)
		if input.UserInputType == Enum.UserInputType.Keyboard then
			if input.KeyCode == Enum.KeyCode.Return then
				print("Client Has Entered There Message")
				ServerConnect:FireServer()
				
				local MsgTempClone = MessageTemplate:Clone()
for index, element in pairs(ChatLogs:GetChildren()) do
      if element.Name = "MessageTemplate" then
        warn("MessageTemp already exsists")
        —— code goes here
else
       MsgTempClone.Parent = ChatLogs
       warn("Inserted Clone")
      end
end
				MsgTempClone.Text = TXTbox.Text
				TXTbox.Text = ""
			end
		end
	end)
end)

This doesn’t seem to fix the problem. hmmm.

Try cleaning up the InputBegan function and see what that does.

TXTbox.FocusLost:Connect(function()
local Connection
Connection = UIS.InputBegan:Connect(function(input, gameProcessedEvent)
		if input.UserInputType == Enum.UserInputType.Keyboard then
			if input.KeyCode == Enum.KeyCode.Return then
				print("Client Has Entered There Message")
				ServerConnect:FireServer()
				
				local MsgTempClone = MessageTemplate:Clone()
for index, element in pairs(ChatLogs:GetChildren()) do
      if element.Name = "MessageTemplate" then
        warn("MessageTemp already exsists")
        —— code goes here
else
       MsgTempClone.Parent = ChatLogs
       warn("Inserted Clone")
      end
end
				MsgTempClone.Text = TXTbox.Text
				TXTbox.Text = ""
                        Connection:Disconnect()
			end
		end
	end)
end)

Yeah it looks like you’re connecting another InputBegan event every time the FocusLost event is called, cleaning up your event connections like what @BGMMasterYTReal suggested above will work

I don’t know how these specific events work but it also just seems like bad practice to be doing what you’re doing in this way, maybe remove the FocusLost event in favor of using TextBox:IsFocused() within the InputBegan event

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.