How do I make a sound stop when someone touches a part?

I am working on this game where there is an ambient sound that is supposed to play only when you’re inside a building. I have tried using sound rolloff but the sounds get layered and sounds super glitchy.

local Part = --Part location
local Sound = --Sound location

Part.Touched:Connect(function()
    Sound:Stop()
end)

How about use this example?

if Sound.IsPlaying == false then
  Sound:Play()
else
  return
end

I kinda bad on coding, so if its not work, please go and find help to some good scripters

It’d be best to use FindPartsInRegion3 for that scenario though, in my opinion.

My code looks like this, but nothing happened. Are there any errors

local Part = workspace
local Sound = workspace

	Part.Touched:Connect(function()
		Sound:Stop()
	end)

workspace isn’t the Part or Sound, is it?

Thank you, I fixed my error in my script.

1 Like

I wrote the whole script, that might be working

-- Put script into your part
local YourPart = script.Parent
local Sound = -- Your sound location
 YourPart.Touched:Connect(function(BodyPart)
  if BodyPart.Parent:FindFirstChildWhichIsA("Humanoid") then
  if Sound.IsPlaying == false then
  Sound:Play()
else
return
end
end
end)

YourPart.TouchEnded:Connect(function(BodyPart)
  if BodyPart.Parent:FindFirstChildWhichIsA("Humanoid") then
if Sound.IsPlaying == true then
  Sound:Stop()
else
  return
end
end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.