Sound play more than once when playing sound locally if a player touches a part

script.Parent.Touched:Connect(function(Touch)
	local player = game:GetService("Players"):GetPlayerFromCharacter(Touch.Parent) 
	if player then
		local DB = false
		if DB == false then
		game.SoundService:PlayLocalSound(game.SoundService.SpotSound)
			local DB = true
		end
	end
end)

so I have this script (RunContext = Client) that plays a sound if a play touches a part but the problem is that the sound doesn’t plays once I tried making a debounce to prevent looping but nothing changes (Note: I have another script that do another thing if a play touches the part but it plays on the server not the client (RunContext = Legacy) but It doesn’t plays a sound tho

Make sure you add a wait right after it plays the sound into your code, so the db variable you made can actually prevent players from activating it without a cooldown. Right now, there is nothing halting the code, so it is just constantly changing the db value and playing the sound.

1 Like

Nothing changed still the same problem I made the script like this btw

local DB = false
script.Parent.Touched:Connect(function(Touch)
	local player = game:GetService("Players"):GetPlayerFromCharacter(Touch.Parent) 
	if player then
		wait(0.1)
		if DB == false then
		game.SoundService:PlayLocalSound(game.SoundService.SpotSound)
			local DB = true
			wait(0.1)
		end
	end
end)

Make sure you wait before you reset your db variable, It is still going through that same loop, just with an extra 0.2 second wait.

The same I changed the wait to wait(0.3) but It is the same I will use print to see the debounce and see if there is a problem in it

You’re redefining your DB variable by adding local in front of it.
This should work now.

local DB = false

script.Parent.Touched:Connect(function(Touch)
	local player = game:GetService("Players"):GetPlayerFromCharacter(Touch.Parent) 

	if player then
		if DB == false then
		    game.SoundService:PlayLocalSound(game.SoundService.SpotSound)

			DB = true
		end
	end
end)

I think I am using the debounce on a wrong way I will edit it and see

Yea I edit it before but it is the same

No your edit is still different.
Just try mine

I noticed that we I added the wait and edit it

It worked but when the player touches the part again the sound does not play

I will try this

local DB = false
script.Parent.Touched:Connect(function(Touch)
	local player = game:GetService("Players"):GetPlayerFromCharacter(Touch.Parent) 
	if player then
		if DB == false then
			DB = true
		    game.SoundService:PlayLocalSound(game.SoundService.SpotSound)
			DB = false
		end
	end
end)
local DebounceTime = 3
local DB = false

script.Parent.Touched:Connect(function(Touch)
	local player = game:GetService("Players"):GetPlayerFromCharacter(Touch.Parent) 

	if player then
		if DB == false then
		    game.SoundService:PlayLocalSound(game.SoundService.SpotSound)

			DB = true

            task.delay(DebounceTime , function()
                DB = false
            end)
		end
	end
end)

Change debounce time to however long you want it to wait

May I ask how often you want the sound to play? If it is every time they step on it, then I would suggest waiting for the player to stop touching the part (using TouchEnded), so it doesn’t play multiple times while you are still on the part.

Otherwise if you use the debounce approach, it is going to be for however long you want the delay to be.

1 Like

Yea you are correct debounce wait work but It is better to use TouchEnded on my situation ok I will try it

Try this:

script.Parent.Touched:Connect(function(Touch)
	local player = game.Players:GetPlayerFromCharacter(Touch.Parent) 
	if player then
		local DB = false
		if DB == false then
		game.SoundService:PlayLocalSound(game.SoundService.SpotSound)
			local DB = true
        else
            print("DB = true")
		end
	end
end)
1 Like

I want the sound to plays everytime the player touches the part not just once (The problem is the sound plays more than once on each touch)

Use this in your script Sound.Ended:Wait()

1 Like

ok I will try this :+1: :+1: :+1:

1 Like

Ok guys I made this script from your suggestions and fixes and yea the sound now plays just once but there is another problem lol :sweat_smile: the sound plays again when the player jump (Btw the part he touches is bigger than the player)

local DB = false
script.Parent.Touched:Connect(function(Touch)
	local player = game:GetService("Players"):GetPlayerFromCharacter(Touch.Parent) 
	if player then
		if DB == false then
			DB = true
			game.SoundService:PlayLocalSound(game.SoundService.SpotSound)
			script.Parent.TouchEnded:Connect(function(TouchEnd)
				game.SoundService.SpotSound:Stop()
				DB = false
			end)
		end
	end
end)
1 Like