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
4 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:

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?

0
Yeet SpectacularPavlos 86 — 4y

1 answer

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

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.

1
how does this return work? lytew 99 — 4y
1
debounce work* lytew 99 — 4y
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 — 4y
1
oh i understand, thanks man lytew 99 — 4y
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 — 4y
Ad

Answer this question