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