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

How do I stop a sound from playing multiple times when stepping on a brick?

I'm having trouble with a certain script. I don't know how to stop a sound from playing multiple times when you touch a brick. It keeps repeating and I have no answer for this.

This is the whole script v

local Part = script.Parent
local Sound1 = script.Parent.Sound1


Part.Touched:connect(function(hit)
	local hum = hit.Parent:WaitForChild('Humanoid')
	if hum then
		while true do
			Sound1:Play()
			wait(1)
			--wait(0.82800000000000007)
			break
		end
		
	end
end)

p.s I'm new to scripting, so go easy on me.

3 comments

0

It appears you formatted this a bit wrong. Make sure you put your code in between the "~~~" lines.

0

Thank me by accepting my answer

0

Make sure you also aren't looping it! To stop this, disable it in Properties.

1 answer

Once upon a time, there was debounce. Debounce is used to help in these types of situations, by stopping an event from firing multiple times.

local Part = script.Parent
local Sound1 = script.Parent.Sound1
local deb = false


Part.Touched:connect(function(hit)
	local hum = hit.Parent:WaitForChild('Humanoid')
	if hum and deb == false then
		deb = true
		Sound1:Play()
		wait(1)
		deb = false
		Sound1:Stop()
	end
end)

You also had an unnecessary while loop.

3 comments

0

It worked out perfect, thanks.

0

Glad I could help

0

Accept his answer.

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