Ok I have a Teleporting GUI that has a black screen fade in Teleport the player and then Fade out. I works fine but the problem is that if you spam click the gui it keeps redoing the fading in and teleporting for the amount of times clicked. I want help with making a script that keeps the Player click more then one time.
Debounce
Debounce
is a method used to disable (and I use that term loosely, so all you fellow scripters out there, leave me alone) a block of code from being ran temporarily
, so if something tries to run that block of code again before it finishes
the first time, it simply won't do anything
.
This entire concept is most commonly done with boolean logic
, though technically you could use anything to debounce that section of code as long as you can keep it between two values
.
Example
The point of this is, to make it so the code won't run if it's condition is set to one value, and at the end of the code, change it so it no longer has that barrier. Here's an example:
local Disabled = false local function Test() if Disabled then -- If Disabled is set to true, then... return -- end the code here by returning nothing. end Disabled = true -- if Disabled is false, then the if statement above won't run, which brings us here, allowing us to make "Disabled" true, which is what will stop this function from running again before it's initial call is over. -- Code print("Not disabled") -- How long until we toggle our debounce (allowing the function to be ran again) wait(1) -- Now we remove that barrier after 1 second, so the function may be called again Disabled = false end -- Call this function every half a second (remember our debounce cooldown is 1 second). while wait(0.5) do spawn(Test) -- "spawn" will make it so we can run the code inside the function while also moving to the next line of code, allowing us to run multiple things at once (much like events for objects). Though, the logic behind this goes into explaining coroutines, which you should save for another time. end
You will see that the code above will only print Not disabled
every 1 second, instead of every half second (at the speed our loop runs the code).
You can use this exact same method in your script, perhaps toggling your debounce
after your for loop (or whatever loop you're using) has ended (for your GUI transparency transitioning).
Sources
More on debounce
: http://wiki.roblox.com/index.php?title=Debounce
The spawn
function: http://wiki.roblox.com/index.php?title=Function_dump/Functions_specific_to_ROBLOX#Spawn
More on coroutines
: http://wiki.roblox.com/index.php?title=Beginners_Guide_to_Coroutines#Spawn
If you have any questions, or need any help, just let me know.
An easier to read answer This code allows you to make an object when you press z every wait(3) seconds
Active = true function onKeyPressed(input,Gamestuff) if input.KeyCode == Enum.KeyCode.Z then if Active then local g= Instance.new("Part",game.workspace) -- Space g.CFrame = game.Players.LocalPlayer.Character.Torso; Active = false; wait(3)-- debounce effect g:Destroy()-- destroy part before set active to true Active = true end --end of the debounce if end-- end of if for key Code end-- end of function game:GetService("UserInputService").InputBegan:connect(OnkeyPressed)