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

how to avoid spamming an event?

Asked by
lytew 99
5 years ago

I created a script that makes a humanoid say something when someone comes close, everything works fine, but the problem is that the humanoid is repeated the sound infinite times when a player gets close. I would like him to speak only once and then keep repeating after 5 seconds he finished speaking, can anyone help? another problem is that the sound is played before the event happens, watch the script: script:

1local scream = "rbxassetid://292124372"
2local works = game:GetService("Workspace")
3local hitboxx = works.scream.hitbox
4hitboxx.Touched:connect(function(h)
5local sound = Instance.new("Sound", hitboxx.Parent.Head)
6sound.SoundId = scream
7 sound:Play()
8 
9end)

something wrong?

0
Yeet SpectacularPavlos 86 — 5y

1 answer

Log in to vote
0
Answered by
Torren_Mr 334 Moderation Voter
5 years ago

You need to make a debounce.

01local scream = "rbxassetid://292124372"
02local works = game:GetService("Workspace")
03local hitboxx = works.scream.hitbox
04local cooldown = 5 --how many seconds to wait before doing it again
05local debounce = false
06hitboxx.Touched:connect(function(h)
07if debounce == false then --checking if debounce is false
08debounce = true --making debounce true
09local sound = Instance.new("Sound", hitboxx.Parent.Head)
10sound.SoundId = scream
11 sound:Play()
12wait(cooldown) --waiting the cooldown
13debounce = false --making the debounce false again
14end
15end)

The script should work, if you got any questions ask me.

1
how does this return work? lytew 99 — 5y
1
debounce work* lytew 99 — 5y
0
You have a value, the script checks if the value is false, if it is, then the script changes it to true, so the script can't repeat until the value is false again, the script waits the time and changes it to false so the script could repeat again. Torren_Mr 334 — 5y
1
oh i understand, thanks man lytew 99 — 5y
1
it was bad there, I can't click that little arrow up next to your question, if it didn't help you lytew 99 — 5y
Ad

Answer this question