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