Wrong Sound Playing when outside a building

Alright so I have a problem with my game so basically I want to play a specific sound when I’m going inside the INN building but when I’m outside I want it to play another sound. So right now, I did a thing which was basically ramping up and down the audio once I achieve and getting closer to a specific part my two parts in the example are INNRegion and INNRegionGateway

Here’s the script that ramp up and down the audio

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local SoundsService = game:GetService("SoundService")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local rootPart = character:WaitForChild("HumanoidRootPart")

local innRegion = workspace:WaitForChild("INNRegion")
local gateway = workspace:WaitForChild("INNRegionGateway")
local pineForestCenter = workspace:WaitForChild("MapSpawns"):WaitForChild("PineForestSpawn")

local wallsFolder = workspace:WaitForChild("MapWalls"):WaitForChild("PineForest")
local wallParts = {
	wallsFolder:WaitForChild("Wall1"),
	wallsFolder:WaitForChild("Wall2"),
	wallsFolder:WaitForChild("Wall3"),
	wallsFolder:WaitForChild("Wall4")
}

local music = SoundsService:WaitForChild("ADVENTURER INN")
local musicOutside = SoundsService:WaitForChild("DEFAULT SONG outside")
local pineTreeSoundFolder = SoundsService:WaitForChild("Pine tree sound")
local pineMusic = pineTreeSoundFolder:WaitForChild("PineTree Musique")

local maxVolume = 1
local maxDistance = (innRegion.Position - gateway.Position).Magnitude

local fadeOutBuffer = 5
local pineTriggerDistance = 1550

music.Looped = true
musicOutside.Looped = true
pineMusic.Looped = true

music.Volume = 0
musicOutside.Volume = 0
pineMusic.Volume = 0

music:Play()
musicOutside:Play()
pineMusic:Play()

local hasEnteredInn = false

local function smoothStep(x)
	return 1 - math.exp(-x * 10)
end

local function updateCharacterRefs()
	character = player.Character or player.CharacterAdded:Wait()
	rootPart = character:WaitForChild("HumanoidRootPart")

	local distanceFromInn = (rootPart.Position - innRegion.Position).Magnitude
	if distanceFromInn < maxDistance then
		hasEnteredInn = true
		music.Volume = maxVolume
		musicOutside.Volume = 0
		pineMusic.Volume = 0
	else
		hasEnteredInn = false
	end
end

player.CharacterAdded:Connect(function()
	updateCharacterRefs()
end)

updateCharacterRefs()

RunService.RenderStepped:Connect(function()
	if not character or not rootPart then return end

	local playerPos = rootPart.Position
	local distanceFromCenter = (playerPos - innRegion.Position).Magnitude
	local distanceFromPine = (playerPos - pineForestCenter.Position).Magnitude
	local distanceFromGateway = (playerPos - gateway.Position).Magnitude
	local adjustedDistance = distanceFromGateway - fadeOutBuffer
	local fadeRatio = math.clamp(adjustedDistance / (maxDistance - fadeOutBuffer), 0, 1)
	local easedFade = smoothStep(fadeRatio)

	-- Détection d'entrée/sortie du INN
	if distanceFromCenter < maxDistance then
		if not hasEnteredInn then
			print("Player has ENTERED the INN")
			hasEnteredInn = true
		end
	else
		if hasEnteredInn then
			print("Player has EXITED the INN")
			hasEnteredInn = false
		end
	end

	local function isPlayerBetweenWalls()
		local minX, maxX = math.huge, -math.huge
		local minZ, maxZ = math.huge, -math.huge
		for _, wall in ipairs(wallParts) do
			local wallPos = wall.Position
			minX = math.min(minX, wallPos.X)
			maxX = math.max(maxX, wallPos.X)
			minZ = math.min(minZ, wallPos.Z)
			maxZ = math.max(maxZ, wallPos.Z)
		end
		return playerPos.X >= minX and playerPos.X <= maxX and playerPos.Z >= minZ and playerPos.Z <= maxZ
	end

	if isPlayerBetweenWalls() then
		pineMusic.Volume = maxVolume
		musicOutside.Volume = 0
		music.Volume = 0
	else
		if hasEnteredInn then
			music.Volume = maxVolume
			musicOutside.Volume = 0
			pineMusic.Volume = 0
		else
			music.Volume = (1 - easedFade) * maxVolume
			musicOutside.Volume = easedFade * maxVolume
			pineMusic.Volume = 0
		end
	end
end)

As you can see it plays the Inn music on the outdoor

why not use a hitbox with get parts in part?

you can customise the exact hitbox with it

your solution, which is using range (I think) basically creates a huge circle, which is probably different to what you want exactly

1 Like

To have the sound play just inside the inn put a Transparent, CanCollide false box the same shape as the inn but about 2 studs inside the walls.
Put the sound in the Part.
Set your MinRollOffDistance to 0 and MaxRollOffDistances to 4. (You can adjust the Max to adjust for the thickness of your walls, I just guessed they were about 2 studs thick
This way there won’t be a sharp change in sound, but depending how fast the player is moving it should go from no sound to full sound in the 4 studs from the outer wall into the building.

As for the ambient sound outside have it playing on the Client, not the server. You can use GetPartsInPart (not in the documentation as far as I can see, but there are quite a few posts about it here in the forums if you search it) to see if the player is inside the inn and shut off the outdoor sound for that player. When they leave the Part have the outdoor sounds play again.

All players would hear the sound inside the inn without it having it scripted.