(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.
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.
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()
This might be kinda unrelated, but what’s the difference between MouseButton1Up and MouseButton1Click? (Also, using MouseButton1Up doesn’t seem to work. Same issue.)
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)
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).
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
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
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 scriptmore 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.
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
With good indentation, it should be much easier to see what is inside a loop and other things
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.