Stopping input function

I have a few buttons and when they are clicked they activate an input function for the mouse, but when you press the button multiple times the function multiplies, which I don’t want I want it to be removed when the button is pressed again, let me show you:

for _, v in pairs(towersFrame:GetChildren()) do 
	if v:IsA("TextButton") then
		v.MouseButton1Click:Connect(function()
			UIS.InputBegan:Connect(function(input) 
				if input.UserInputType == Enum.UserInputType.MouseButton1 then
					
					-- maybe :Disconnecting, but idk how
				end
			end
		end)
	end
end
3 Likes

You can add an attribute to the button:

v:SetAttribute("Connected", true)

and check if it is true before the event is connected:

if v:GetAttribute("Connected") then return end

If you specifically need the event to be disconnected and reconnected, try:

local Connections = {}

for _, v in pairs(towersFrame:GetChildren()) do 
	if v:IsA("TextButton") then
		v.MouseButton1Click:Connect(function()
			if Connections[v:GetFullName()] then
				Connections[v:GetFullName()]:Disconnect()
			end
			Connections[v:GetFullName()] = UIS.InputBegan:Connect(function(input) 
				if input.UserInputType == Enum.UserInputType.MouseButton1 then
					-- ...
				end
			end
		end)
	end
end
3 Likes

You are checking if the person click the button 2 times! Remove the UIS event

Hi! Here’s solution

for _, v in pairs(towersFrame:GetChildren()) do 
	if v:IsA("TextButton") then
		v.MouseButton1Click:Wait()
		print("Did Something")
	end
end

We just wait until player press the button and doing something

It’s a placement system, when the player clicks the button the part he wants to place follows the mouse but when the player clicks after that it should place the part, and then disconnect or remove the funcion, but you cant do that.

Maybe adding a debounce to it?

Try to add :once instead of connects so that it auto disconnects afterwards, put the UIS outside of the button event and add a boolean variable that if its true then it does the placement

Thanks for you’re help , but I found a solution:

local connection2
connection2 = UIS.InputBegan:Connect(function(input) 
    connection2:Disconnect()
end

it does not work when you do:

local connection2 = UIS.InputBegan:Connect(function(input) 
    connection2:Disconnect()
end

What is the point of you doing this?

What do you mean, giving the solution myself or making the post?

Embedding a UserInputService.InputBegan event listener for detecting a single left-mouse click within a GuiButton.MouseButton1Click event listener

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