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
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
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.
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