Audio when Part is Touched

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    sound on touch of a part but only once of that sound and no more

  2. What is the issue? Include screenshots / videos if possible!
    When I touch the part it keeps repeating the noise if I walk all over it but I only want it to play it once

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Ive looked at youtube videos but they werent clear abt the part im looking for

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

this is the script:

local part = script.Parent

part.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") ~= nil then
		local sound = script.Parent.SonictheHedgehogSFX

		if not sound.IsPlaying then
			sound:Play()
		end
	end
end)

Please help thanks.

local part = script.Parent
local CanPlay = true

part.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") ~= nil then
	    if CanPlay == true then 
	        CanPlay = false 
	        
    		local sound = script.Parent.SonictheHedgehogSFX
    
    		sound:Play()
    		task.wait(sound.TimeLength)
    		CanPlay = true 
	    end
	end
end)

1 Like

that wasnt quite what I was looking for but I solved it with the help of it because the sound is 1 - 2 seconds long it still gave almost the same result so all I did was change the task.wait(sound.TimeLength) to task.wait(100) which just makes it takes 100 seconds before u can go and repeat it again but I wanna know a way to make that 100 second into like infinite I guess basically just not letting them play the noise at all after the first time.

local part = script.Parent
local CanPlay = true

part.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") ~= nil then
	    if CanPlay == true then 
	        CanPlay = false 
    		local sound = script.Parent.SonictheHedgehogSFX
    
    		sound:Play()
	    end
	end
end)

4 Likes

You can add an debounce for that.

Like this:

local part = script.Parent
local debounce = false

part.Touched:Connect(function(Hit)
if debounce == false then -- if its true it wont get trough this
debounce = true
	if Hit.Parent:FindFirstChild("Humanoid") == true then
		local sound = script.Parent.SonictheHedgehogSFX

			sound:Play()
debounce = false -- here it sets the debounce back to false so it can work again
        end
	end
end)
1 Like

What if you can include a task.Wait() In the Debounce? Would that make it take more time and be more secure from being run at high speeds?