Audio detection when stepping in or outside a building

Heya!

Throughout my game development, I have built a lobby that acts like a resort and I am working on enhancing its environment details. One of those details being is audio. I have an audio playing in the player’s PlayerGui where it just plays ocean noises.

When entering in and out of the main building, the ocean audio remains at the same volume level. Obviously I don’t want this, I want to create the perspective of actually being inside a building.

I have built barriers inside the building and named the group appropriately while using the :GetPartsObscuringTarget(). So whenever the camera is inside the barriers, it the following would lower the volume level.

Barriers selected in the image:

I attempted this as a water ambient script in one of my previous projects and it worked successfully. However, it doesn’t seem to be working too well here.

LocalScript in StarterGui:

local group = workspace.AudioModifier_Group -- group of barriers
local audio = script.Parent.sea --audio

while true do
	wait(0.025)
	local castPoints = {
		group.BarrierOne.Position;
		group.BarrierTwo.Position;
		group.BarrierThree.Position;
		group.BarrierFour.Position;
	}
	local ignoreList = {}
	
	local parts = workspace.CurrentCamera:GetPartsObscuringTarget(castPoints,ignoreList)
	
	local inLobby = false
	
	if #parts > 0 then
		for _, c in pairs(parts) do
			if c.Name == "BarrierOne" or "BarrierTwo" or "BarrierThree" or "BarrierFour" then
				inLobby = true
				break
			end
		end
	end
	
	if inLobby then
		audio.Volume = 0.015
	else
		audio.Volume = 0.2 --Volume is normally 0.2 when outside
	end
end

Upon joining the server, I am greeted with constant fluctuated volume of the ocean audio no matter where my camera is pointing at. It keeps bouncing from the audio points 0.2 and 0.015, which is very jarring.

How can I counter this? :sweat_smile:

Modify this for your situation

You could put a non-collidable part(s) inside the building. Then, write a script where when it is touched, it will get the sound and lower the volume. When the touch ends, it reverts to the original volume. Also, you can use region3 but I don’t like those.

idk if this works:
(script in non collidable part)


script.Parent.Touched:Connect(function(part)
if part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid") then 
local player = game.Players:FindFirstChild(part.Parent.Name)
local audio = player:WaitForChild("sea")

if audio.Volume == 0.02 then
audio.Volume = 0.015
end
end
end)

script.Parent.TouchedEnded:Connect(function(part)
if part.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid") then 
local player = game.Players:FindFirstChild(part.Parent.Name)
local audio = player:WaitForChild("sea")

if audio.Volume == 0.015 then
audio.Volume = 0.2
end
end
end)

I’ve considered doing Touched, but ultimately using Touched functions just feels so fake for this particular case. Additionally, TouchedEnded is very unreliable and I’ve seen multiple threads pop up where it causes bugs or strange results.