A Debounce script prevents a script from being repeated before it can finish.
Let's say you have a button on the floor, and you need it
to print something into the console or complete an action
when someone steps on it. This is how it would look normally.
game.Workspace.Button.Touched:Connect(function(hit)
print ("Button Pressed!")
wait(5)
print ("This action has been completed!")
end)
Although this seems like it would work, this is what would
be outputted.
Button Pressed!
Button Pressed!
Button Pressed!
Button Pressed!
Button Pressed!
This action has been completed!
This action has been completed!
This action has been completed!
This action has been completed!
This action has been completed!
Fixing this is pretty simple, all it takes is adding a
couple more lines to the script.
local buttonPressed = false
--Saves whether or not the button is pressed
Workspace.Button.Touched:Connect(function(hit)
if not buttonPressed then
--Is it not pressed?
buttonPressed = true
--Marks it as pressed, so that nothing else will execute
print ("Button Pressed!")
wait(5)
print ("This action has been completed!")
buttonPressed = false
--Mark it as not pressed, so the script can execute again
end
end)
You can also add a wait command at the end so the script will
wait before it can be re-activated.
This little tutorial was paraphrased by me so it could be added
into this game, here's a link.
https://developer.roblox.com/articles/Debounce
Go check it out if you want a more in depth tutorial on a debounce
script!