Hi there! I was working on an experience called “Box Launching Simulator”. The goal of the game is to launch a yellow rock as far as possible. I added a sliding sound effect to the rock to make it more realistic. However, I encountered a problem with the impact sound. Whenever the rock slides on the ground, the impact sound repeats excessively, which is very frustrating for me.
Here’s the Script:
local soundTable = script.Sounds:GetChildren()
script.Parent.Touched:Connect(function(hit)
if script.Parent.Velocity.Magnitude >= 20 then
local randomSound = soundTable[math.random(1, #soundTable)]
local clonedSound = randomSound:Clone()
clonedSound.Parent = script.Parent
clonedSound:Resume()
script.Parent.Slide:Resume()
wait(3)
clonedSound:Destroy()
end
end)
script.Parent.TouchEnded:Connect(function(hit)
script.Parent.Slide:Pause()
end)
while task.wait() do
script.Parent.Slide.Volume = script.Parent.Velocity.Magnitude / 50
end
You can add a condition to check if the sound is already playing before playing it again.
local soundTable = script.Sounds:GetChildren()
script.Parent.Touched:Connect(function(hit)
if script.Parent.Velocity.Magnitude >= 20 then
local randomSound = soundTable[math.random(1, #soundTable)]
local clonedSound = randomSound:Clone()
clonedSound.Parent = script.Parent
if not clonedSound.IsPlaying then
clonedSound:Resume()
script.Parent.Slide:Resume()
wait(3)
clonedSound:Destroy()
end
end
end)
script.Parent.TouchEnded:Connect(function(hit)
script.Parent.Slide:Pause()
end)
while task.wait() do
script.Parent.Slide.Volume = script.Parent.Velocity.Magnitude / 50
end
if not clonedSound.IsPlaying then checks if the sound is already playing. If it is not, then it plays the sound. If it is, then it does nothing and prevents the sound from playing again. This should stop the sound from repeating excessively.
A simple question, but do you have the Sound.Looped property set to true?
The problem with Touched events is that they fire multiple times in a case like yours. It’s really obvious with Touched events using Humanoids too.
That’s why the others have suggested a debounce system that tells the script to bypass itself when the Touched event fires as the rock continues to fire the Touch and TouchEnded every split second.
local soundTable = script.Sounds:GetChildren()
local DebTick = 0
script.Parent.Touched:Connect(function(hit)
if (tick() - DebTick) >= 0.5 then DebTick = tick() else return end
if script.Parent.Velocity.Magnitude >= 20 then
local randomSound = soundTable[math.random(1, #soundTable)]
local clonedSound = randomSound:Clone()
clonedSound.Parent = script.Parent
clonedSound:Resume()
script.Parent.Slide:Resume()
wait(3)
clonedSound:Destroy()
end
end)
script.Parent.TouchEnded:Connect(function(hit)
script.Parent.Slide:Pause()
end)
while task.wait() do
script.Parent.Slide.Volume = script.Parent.Velocity.Magnitude / 50
end
Also by the way you shouldn’t just ask people to fix your script, the point is to learn something new that you will be able to use in programs in the future.