Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does my ALL my OnTouched function spam?

Asked by
AyeJude 41
6 years ago
Edited 6 years ago

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

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

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.

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 Tomstah 401 — 6y
0
Thanks for explaining it better, Tomstah, I’m on mobile so typing that terrible explanation was tricky. cmgtotalyawesome 1418 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago
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 :)

Answer this question