MouseButton1Down fires more than once [SOLVED]

Hello DevForum!

(I’m not too good at scripting, so if you can I’d like to be explained to like I’m five or given resources on some of this stuff.)

So, I’m just trying to fix a few issues with my current system. This excerpt of the script teleports the player, makes two GUIs invisible, prints random spam, then calls a random function’s spammy name.

script.Parent.Parent.Parent.TextButton.MouseButton1Down:Connect(function()
	local player = game.Players.LocalPlayer
	player.Character.HumanoidRootPart.CFrame = CFrame.new(-2.2, 23.699, -52.117)
	script.Parent.Parent.Parent.TextButton.Visible = false
		script.Parent.Parent.Visible = false
		script.Parent.Parent.Text = ""
		print("waodhfa")
		akirpap()

The problem is, every time I do this, pressing the button fires multiple times depending on how long you hold. I don’t want this. I’ve tried putting MouseButton1Down:Disconnect(), but when doing it on a Localscript, which this code is on, I get the error: “Disconnect is not a valid member of RBXScriptSingal.”

I haven’t found any other solutions online, but I definitely just didn’t look into this hard enough. If you need any sort of clarification, don’t hesitate to tell me.

Thanks for reading!

Any suggestions or resources would be appreciated!

To clarify, there’s no actual reason why I need MouseButton1Down. If you recommend me using something different, say that.

3 Likes

is this a local script, module script, or server script?

It’s a LocalScript. This is just an excerpt of it.

1 Like

Hello, try using an empty task.wait() whenever it’s clicked. I had functions spamming before, using wait solved the thing for me. I hope it works for you too. Put the task.wait() above the actions performed when the button is clicked.

All you need to do is
local MouseButtonDown
MouseButtonDown = put the mouse button down function here
at the end of the function just do MouseButtonDown:Disconnect()

you need to put the function in a variable so the script can see it, then you can just disconnect it

I’m not entirely sure about what you mean, but if you mean this:

local MouseButton1Down
					MouseButton1Down = 
						script.Parent.Parent.Parent.TextButton.MouseButton1Down:Connect(function()
					local player = game.Players.LocalPlayer
					player.Character.HumanoidRootPart.CFrame = CFrame.new(-2.2, 23.699, -52.117)
					script.Parent.Parent.Parent.TextButton.Visible = false
						script.Parent.Parent.Visible = false
						script.Parent.Parent.Text = ""
						print("waodhfa")
							akirpap()
							MouseButton1Down:Disconnect()

Then the code works, but the issue is still there. Badly.

honestly, that’s very weird, i’ve never seen anything like that

Try using MouseButton1Up instead or use a debounce.

I have two questions:

  1. This might be kinda unrelated, but what’s the difference between MouseButton1Up and MouseButton1Click? (Also, using MouseButton1Up doesn’t seem to work. Same issue.)

  2. What’s a debounce? Upon looking it up, it seems like there’s many ways to do it and I’m not sure what the differences are. If you can find me resources that’ll tell me, I’d be happy to read them.

If you’re registering a Button click event, use .Activated since it handles mobile as well, as opposed to MouseButton1Down which only registers PC (& mouse-enabled devices)

MouseButton1Down fires right when the player clicks down on their mouse.

MouseButton1Up fires when the players clicks down on their mouse and let’s go.

Here’s a good resource on debounces by Roblox: Debounce Patterns | Documentation - Roblox Creator Hub

Debounces basically make sure a function doesn’t run faster than it’s suppose to.

local Debounce = false
local DebounceWait = 5


Button.MouseButton1Up:Connect(function()
if Debounce ~= true then -- If the Debounce is true then we don't continue the function
Debounce = true

-- Do tasks here

task.wait(DebounceWait)
Debounce = false -- Reset the debounce value so the function can be run again
end)

In regards to your code though if these aren’t working, I’m not too sure. I would suggest starting with restructuring the script first so it’s easier to read and debug. For example, you should have the GUI button/frame and player value as a variable instead. I don’t really see anything obvious in the code you provided (assuming that the lack of an end) is just an accident).

1 Like

or use :Once() instead of :Connect() since :Once() automatically disconnects itself

1 Like

Never had such an issue with MouseButton1Down firing multiple times by holding the mouse button down (unless broken mouse)

For disconnecting, you need to do it like this

local Connection = Mouse.Button1Down:Connect(function() 
	
end)

Connection:Disconnect()

Basically, when you connect to a RBXScriptSignal (aka event) with :Connect(), a RBXScriptConnection is returned

That object has 1 method, and 1 property, including the Disconnect() method

1 Like

I’m trying to get the fastest ones first, so just saying: that doesn’t work. I don’t know why, but y’know…

If this is what was intended, it didn’t work. The button did nothing. I know for a fact that I did something you didn’t intend, but I can’t tell what. (I assumed that “Mouse.Button1Down” was a typo, which is why it’s not the same here)

					local Connection = script.Parent.Parent.Parent.TextButton.MouseButton1Down:Connect(function() 
							local player = game.Players.LocalPlayer
							player.Character.HumanoidRootPart.CFrame = CFrame.new(-2.2, 23.699, -52.117)
							script.Parent.Parent.Parent.TextButton.Visible = false
							script.Parent.Parent.Visible = false
							script.Parent.Parent.Text = ""
							print("waodhfa")
						akirpap()
				end)
				Connection()
				Connection:Disconnect()

Mouse.Button1Down is from the MouseObject, I did not read with enough care lol

However, regardless of the event, RBXScriptSignals work the same

So the way you’ve done it should work, if this function call wouldn’t be there
image
Well, it will disconnect the event right away, so it will “work” but it will be disconnected before you have time to click it

Do you want do be able to click only once or you just don’t want it to be activated constantly?
For guis, I usually use MouseButton1Click, maybe that would work idk

Thanks for the help everyone!

I’m sorry for giving everyone a bit of a headache, because it was my own, mind-numbingly, ginormously stupid mistake.

If I had shown the entire script, even someone who just started coding would get it. Basically, when I was polishing up the script so that it would be easier to understand and edit, I discovered that I had accidentally situated my ends in a way that put the function inside of a while loop, on top of the function inside of the function at the end linking itself back to the button, making it multiply after all of that. Result?

Simply putting my snippet / my ends in the wrong place lead to a simple button click looping a script more than 5000 times.

There are a lot of dumb scripting errors that are due to me not looking at everything or making tiny mistakes, but I never expected that to be the solution to a seemingly catastrophic error. Sorry again, but also thanks again! This community is surprisingly kind and I look forward to getting a real problem solved in the future.

It’s safe to say I won’t be making that mistake again.

I’ll be marking JoeyKreates’ answer as the solution, because I both want to make it clear that this has been solved and that their response, saying that

was the thing that fixed my problem. Sure, the problem wasn’t really about debounces and the differences in syntax, but them telling me to start with that got me to look at the function’s place in the script, and saved me tons of head-smacking later on.

1 Like

God, that error is legendary lmao

Wouldn’t call that stupid, though, I have noticed that the indentation of the snipped you provided is a bit weird. If the indentation in your code is weird, there’s a button that can apply the correct indentation to the whole script
image
With good indentation, it should be much easier to see what is inside a loop and other things

1 Like

Thank you! I sometimes change the indentation in the short term for whatever reason and then forget what it is later, and it makes the script kind of unreadable. This is gonna help loads.