MouseButton1Click disconnecting not working as intended

  1. What do you want to achieve? Keep it simple and clear!
    I want for there to only be allowed one connection at a time, instead of them stacking.

  2. What is the issue? Include screenshots / videos if possible!
    When the connection disconnects it doesn’t work again.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    A lot of research and going on Events

local button = script.Parent
local remoteEvent = game.ReplicatedStorage.remoteEvents.boothEvents.clickedBoothButton
local connection

local function onClicked()
	local layoutOrder = button.LayoutOrder
	remoteEvent:FireServer(layoutOrder, button.Name)
	connection:Disconnect()
end

connection = button.MouseButton1Click:Connect(onClicked)
3 Likes

If you want it to work again you need to connect it.

4 Likes

Just like @bytesleuth said. Once you disconnect a connection, it will no longer work. It needs to stay connected, otherwise it won’t have any function to run since it’s not connected with one.

3 Likes

Should I do something else than disconnecting it then? I just don’t want it to be stacking up, it results in it being fired multiple times after one click.

3 Likes

Use :Once() instead of :Connect(). It will disconnect automatically after it is fired one time.

2 Likes

I tried that, but it does the same thing, I want it to still be able to be clicked after it is clicked once. Let me explain better.

Click Once
printed: Clicked

Clicked Again
printed: Clicked
printed: Clicked

The events are stacking on eachother, and I want it like this

Click Once
printed: Clicked

Click Again
printed: Clicked

1 Like

What are you wanting to achieve? Connecting the event after X time or?

1 Like

Click Once
printed: Clicked

Clicked Again
printed: Clicked
printed: Clicked

Click Once
printed: Clicked

Click Again
printed: Clicked

1 Like

If this was a singular connection, this shouldn’t be occurring, are you connecting it somewhere else?

1 Like

No, that’s the only place. 30charrrrrrrrr

1 Like

Would it work if, instead of you running the function directly (…:Connect(onClicked)), you spawned a new thread that ran the function? Maybe this:

button.MouseButton1Click:Connect(function()
	task.spawn(onClicked)
end)

I think spawn() is deprecated but as far as I know it still works

1 Like

It did not work, it still stacks the events.

Try using .Activated instead of that

From what I’ve read you just want to implement a simple debounce.

local debounce = false

local function onClicked()
	if debounce then return end
	debounce = true
	-- insert anything
	task.wait(1) -- set 1 to whatever cooldown
	debounce = false -- re-enables function
end

I’d advise against this because the event is only supposed to run once per click, a bandaid like this isn’t good practice.

Trying this now. charrrrrrrrrr

Sorry it took a while to respond, I did not get the notification you responded. Anyways, this did not work, it has the same effect as the click did.

“I tried that, but it does the same thing, I want it to still be able to be clicked after it is clicked once. Let me explain better.”

I’m pretty sure he just wants to prevent the event from being fired too much. Adding debounce is not bad practice. You should only use :Disconnect or .Once when you really want to have it fired only once. If you keep disconnecting and re-connecting, it is far less efficient, as it would have to be compiled all over again.

I apologize if I don’t explain well, my issue is that the event is stacking ontop of eachother. If I click the button once, it returns as it was clicked once, but after clicking it once if I click it again it returns that it was clicked twice, separate from the first time it was clicked.

1 Like

So you’re saying, in total, if it was clicked 2 times, it would count as if it was clicked 3 times?

1 Like