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
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
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.