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

how to avoid spamming an event?

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:

local scream = "rbxassetid://292124372"
local works = game:GetService("Workspace")
local hitboxx = works.scream.hitbox
hitboxx.Touched:connect(function(h)
local sound = Instance.new("Sound", hitboxx.Parent.Head)
sound.SoundId = scream
 sound:Play()

end)

something wrong?

1 comment

0

Yeet

1 answer

✓ Accepted answer

You need to make a debounce.

local scream = "rbxassetid://292124372"
local works = game:GetService("Workspace")
local hitboxx = works.scream.hitbox
local cooldown = 5 --how many seconds to wait before doing it again
local debounce = false
hitboxx.Touched:connect(function(h)
if debounce == false then --checking if debounce is false
debounce = true --making debounce true
local sound = Instance.new("Sound", hitboxx.Parent.Head)
sound.SoundId = scream
 sound:Play()
wait(cooldown) --waiting the cooldown
debounce = false --making the debounce false again
end
end)

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

5 comments

1

how does this return work?

1

debounce work*

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.

1

oh i understand, thanks man

1

it was bad there, I can't click that little arrow up next to your question, if it didn't help you

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