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

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

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

✓ Accepted answer

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

4 comments

0

debounce makes it that it won’t keep repeating until it ended.

0

@Graze022 I suggest putting the debounce = true on top.

0

what changed here is that you have debounce now. Which will stop your sound from repeating. Hope this helps!

0

@Davydas1118 still does the same thing

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.

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