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

[Solved] Prevent touched event from spamming?

Asked by 5 years ago
Edited 5 years ago

I just tested a simple touched event with a TakeDamage and the humanoid takes damage several times quickly and dies, but only take that damage when they move. I know I can put a denounce but I don't want the function to be spammed repeatedly even with the debounce. I even tested this with changing the baseplate color to random and when I touched the part once, the baseplate changed several random colors in a second. Any ideas for the touched event to happen just once per touch?

0
use Debounce. mixgingengerina10 223 — 5y
0
Alright Ill just stick to debounce for now ScrubSadmir 200 — 5y

2 answers

Log in to vote
2
Answered by
JakyeRU 637 Moderation Voter
5 years ago

Hello. The best option is Debounce. Your function will not be spammed. Here is an example:

Without Debounce

script.Parent.Touched:Connect(function()
    print("Hello World")
end)

With Debounce

Debounce = false

script.Parent.Touched:Connect(function()
    if Debounce == false then
        print("Hello World")
        Debounce = true
        wait(1)
        Debounce = false
    end
end)
Ad
Log in to vote
0
Answered by 5 years ago

There is no optimal way to do that. Debounce is really your best option. However, you can disconnect the event if you wish. This makes it so that the event listener is turned off. This wiki page can explain some elements of a signal/event listener. It is relatively simple. Here is an example:

local connection -- here we name a variable but don't use it. You can name it what you like
connection = script.Parent.Touched:Connect(function() -- does not have to be script.Parent. It will be whatever you are having be touched
    -- do whatever in here
    connection:Disconnect() -- this will turn off the event
end)

If you have any additional questions feel free to post them in the comments under my answer.

Answer this question