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

How do I Prevent .Touched from spamming?

Asked by
Elixcore 1337 Moderation Voter
6 years ago

hello so im having this problem with .Touched, it usually spams because the part that connects fastly disconnects and connects again (cos of it changing size/shape) so I was wondering on how to make it only do something once instead of multiple times.

0
use a boolean and if statement to prevent it from running a second time. #LazyAnswer TheGreatSailor 20 — 6y
0
Try to make a boolean value and attatch it to, the statement, may you include the code you are currently using to possibly further help? ninja_eaglegamer 61 — 6y

2 answers

Log in to vote
1
Answered by 6 years ago

I can provide an example:

local p = script.Parent
local x = true -- our debounce

p.Touched:Connect(function(hit)
    if x then 
        x = false
        -- your code
        wait(5)
        x = true
    end
end)

As simple as that, if x isn't true, it won't run until x is true.

Good luck! If you have trouble inputting the debounce, feel free to ask me!

Wish you luck on your goal!

Ad
Log in to vote
1
Answered by
GingeyLol 338 Moderation Voter
6 years ago
local object = script.Parent
local deb = false
local detime = 5 --debounce time

object.Touched:Connect(function(hit)
    if deb == false then
        deb = true
        print("works!")
        -- your code
        wait(detime)
        deb = false
    end
end)

Answer this question