Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does my ALL my OnTouched function spam?

Every single OnTouched function that I have done always spam, for example, I made a breakable glass the other day, where if you touch it the glass breaks, and it had the breaking part in replicated storage. Once I finished the script, I tested it, and it kept on grabbing the breakable part from replicated storage, and pasting it over and over again.. Any help would be appreciated...

2 answers

✓ Accepted answer

You need to add a debounce to the script, a debounce is just a variable that you set to false before your function so you can check if it’s true before it can run. Say if debounce == false then debounce = true and then do whatever you want to do. To allow the player to break the glass again, just put a wait and then set debounce to false.

2 comments

0

Adding onto it, it basically asks if it's already running. If it's already running then it stops but if it's not then it continues. Here's a full article on it: http://wiki.roblox.com/index.php?title=Debounce

0

Thanks for explaining it better, Tomstah, I’m on mobile so typing that terrible explanation was tricky.

debounce = false

function GlassBreak(hit)
if not debounce and game.Players:GetPlayerFromCharacter(hit.Parent) then
debounce = true
--script--
	end
end

something like that, hope it helps. I'll update it if it is wrong :)

This discussion is preserved as it was. New answers, comments, and votes are closed.