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

How do I make my click for sound script not spam the sound?

Asked by
i_Rook 18
3 years ago

Im trying to make a checkpoint sound, but it keeps on spamming the sound every time the player moves! What should I do?

local SP = script.Parent

function onTouch(hit)
    local H = hit.Parent:FindFirstChild("Humanoid")
    if H ~= nil then
        SP.Sound:Play()
    end
end


    script.Parent.Touched:connect(onTouch)
    script.Parent.TouchEnded = false



2 answers

Log in to vote
0
Answered by
Grazer022 128
3 years ago
Edited 3 years ago

you should use a debounce. Here I’ll show you

local SP = script.Parent
local debounce = false

function onTouch(hit)
   if not debounce then
     local H = hit.Parent:FindFirstChild(“Humanoid”)
     debounce = true
     if H ~= nil and debounce == true then
       SP.Sound:Play()
       wait()
       debounce = false
    end
  end
end)


SP.Touched:Connect(onTouch)
SP.TouchEnded = false
0
debounce makes it that it won’t keep repeating until it ended. Grazer022 128 — 3y
0
@Graze022 I suggest putting the debounce = true on top. Dovydas1118 1495 — 3y
0
what changed here is that you have debounce now. Which will stop your sound from repeating. Hope this helps! Grazer022 128 — 3y
0
@Davydas1118 still does the same thing Grazer022 128 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

You can use a debounce. From what I've seen from the community chat, A debounce is basically a cooldown. When a thing bounces, it keeps going on forever until the original contact is released. A debounce just prevents this from happening over and over again.

local debounce = false --False is deactivated, True is activated
script.Parent.Touched:Connect(function(hit) --You can do this the other way, I just prefer doing it
    if not debounce then --Alternative: If debounce == false
        debounce = true
        if game.Players:GetPlayerFromCharacter(hit.Parent) then --This is to prevent NPCs from touching it
        script.Parent.Sound:Play()
        wait(5) --Change this to any number
        debounce = false
        end
    end
end)

I've added some bits, in order to make the script more neater, and to prevent other things. Edit: I've refined the coding, because I'm not using roblox studio.

Answer this question