hey i have sound regions and i was wondering if there was a way to make the sound fade out as the player walks away. rn if a player steps out, it hard cuts and sounds awful. please help!
This is how I did it, I had a localscript in StartPlayer.StarterPlayerScripts, and would create parts in workspace which are not collidable, then put a Sound under the localscript.
local partsWithAudio = { -- Put your parts/songs here.
{
part = workspace.a,
song = 3235424494
},{
part = workspace.b,
song = 245307850
},{
part = workspace.c,
song = {3235424494 , 245307850}
},
};
local inPart = nil;
local sound = script.Sound;
local cachedLastPart = nil;
local context = nil;
function fadeIn()
for i=sound.Volume, 1, (1-sound.Volume)/10 do
sound.Volume = i;
wait(1/10)
end
end
function fadeOut(done)
for i=sound.Volume, 0, -sound.Volume/10 do
sound.Volume = i;
wait(1/10)
end
if done then
done();
end
end
function enter()
if inPart then
local cachedPart = inPart.part;
local cachedSong = inPart.song;
local canGoToNext = true;
local function playHandleAudio()
if canGoToNext then
if typeof(cachedSong) == "table" then
local onSong = 0;
sound.Looped=false;
for a,b in pairs (cachedSong) do
if typeof(b) == "number" then
sound.SoundId = "rbxassetid://"..b;
sound.TimePosition=0;
sound.Volume = 0;
sound:Play();
fadeIn();
sound.Ended:Wait()
sound:Stop()
end
end
playHandleAudio()
elseif typeof(cachedSong) == "number" then
sound.Looped=true;
sound.SoundId = "rbxassetid://"..cachedSong;
sound.Volume = 0;
sound:Play();
fadeIn();
else
error("Invalid song tpye, must be type of int, or object.")
end
end
end
if sound.IsPlaying then
fadeOut(function()
playHandleAudio()
end);
else
playHandleAudio();
end
return function()
canGoToNext=false;
sound.Looped=false;
fadeOut(function()
sound:Stop();
end)
end
end
end
function exit()
if inPart then
fadeOut(function()
sound.SoundId = 0;
sound:Stop();
end)
inPart = nil;
end
end
function exitIntoNewZone()
fadeOut(function()
local cachedPart = inPart.part;
local cachedSong = inPart.song;
local canGoToNext = true;
local function playHandleAudio()
if canGoToNext then
if typeof(cachedSong) == "table" then
local onSong = 0;
sound.Looped=false;
for a,b in pairs (cachedSong) do
if typeof(b) == "number" then
sound.SoundId = "rbxassetid://"..b;
sound.TimePosition=0;
sound.Volume = 0;
sound:Play();
fadeIn();
sound.Ended:Wait()
sound:Stop()
end
end
playHandleAudio()
elseif typeof(cachedSong) == "number" then
sound.Looped=true;
sound.SoundId = "rbxassetid://"..cachedSong;
sound.Volume = 0;
sound:Play();
fadeIn();
else
error("Invalid song tpye, must be type of int, or object.")
end
end
end
if sound.IsPlaying then
fadeOut(function()
playHandleAudio()
end);
else
playHandleAudio();
end
end);
return function()
canGoToNext=false;
sound.Looped=false;
fadeOut(function()
sound:Stop();
end)
end
end
for a,b in pairs (partsWithAudio) do
spawn(function()
local part = b.part;
local minReigon = part.Position - (0.5 * part.Size)
local maxReigon = part.Position + (0.5 * part.Size)
local reigon = Region3.new(minReigon, maxReigon);
local isIn = false;
local fired = false;
while wait(1/5) do
local parts=workspace:FindPartsInRegion3(reigon);
local inPartLoop = nil;
for c,d in pairs (parts) do
if d.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(d.Parent);
if player == game.Players.LocalPlayer then
inPartLoop = b;
end
end
end
if inPartLoop == nil then
if fired then
fired=false;
if cachedLastPart then
if cachedLastPart.part == b.part == false then
else
inPart=nil;
cachedLastPart=nil;
if context then
spawn(context);
else
spawn(exit);
end
end
else
inPart=nil;
if context then
spawn(context);
else
spawn(exit);
end
end
end
else
if fired == false then
fired=true;
inPart=b;
cachedLastPart=b;
if cachedLastPart then
if cachedLastPart.part == inPartLoop.part == false then
spawn(function()
context=exitIntoNewZone();
end)
else
spawn(function()
context=enter();
end)
end
else
spawn(function()
context=enter();
end)
end
end
end
end
end)
end
do i only put one sound under the script or multiple for each region?
One sound in the script, then you can update what sounds you want to play in the script its self.
okay wait i’m sorry, so which sound is it? does it have to have the specific audio of one of the sound regions?
What do you mean, it should look like this
Then you have parts in Workspace which you are using as your sound reigons. You then update this to include your parts, and songs you want to play.
local partsWithAudio = { -- Put your parts/songs here.
{
part = workspace.partA,
song = 3235424494-- you can pass one song at a time
},{
part = workspace.partB,
song = 245307850
},{
part = workspace.partC,
song = {3235424494 , 245307850} -- you can also pass an array if you'd like
},
};
oh, alright. what if one of the audios that i need uses a roblox audio modifier?
also, is this correct?
{
part = workspace.SoundRegions.Sound1,
song = 6304093764
},{
part = workspace.SoundRegions.Sound2,
song = 332006582
},{
part = workspace.SoundRegions.Sound3,
song = 0
},
};
If the Sound1/2/3 is a part, yes.
okay, i just tested it out and it’s not working. i do have one error.
here’s my current script:
{
part = workspace.SoundRegions.Sound1,
song = 6304093764
},{
part = workspace.SoundRegions.Sound2,
song = 332006582
},{
part = workspace.SoundRegions.Sound3,
song = 0
},
};
local inPart = nil;
local sound = script.Sound;
local cachedLastPart = nil;
local context = nil;
function fadeIn()
for i=sound.Volume, 1, (1-sound.Volume)/10 do
sound.Volume = i;
wait(1/10)
end
end
function fadeOut(done)
for i=sound.Volume, 0, -sound.Volume/10 do
sound.Volume = i;
wait(1/10)
end
if done then
done();
end
end
function enter()
if inPart then
local cachedPart = inPart.part;
local cachedSong = inPart.song;
local canGoToNext = true;
local function playHandleAudio()
if canGoToNext then
if typeof(cachedSong) == "table" then
local onSong = 0;
sound.Looped=false;
for a,b in pairs (cachedSong) do
if typeof(b) == "number" then
sound.SoundId = "rbxassetid://"..b;
sound.TimePosition=0;
sound.Volume = 0;
sound:Play();
fadeIn();
sound.Ended:Wait()
sound:Stop()
end
end
playHandleAudio()
elseif typeof(cachedSong) == "number" then
sound.Looped=true;
sound.SoundId = "rbxassetid://"..cachedSong;
sound.Volume = 0;
sound:Play();
fadeIn();
else
error("Invalid song tpye, must be type of int, or object.")
end
end
end
if sound.IsPlaying then
fadeOut(function()
playHandleAudio()
end);
else
playHandleAudio();
end
return function()
canGoToNext=false;
sound.Looped=false;
fadeOut(function()
sound:Stop();
end)
end
end
end
function exit()
if inPart then
fadeOut(function()
sound.SoundId = 0;
sound:Stop();
end)
inPart = nil;
end
end
function exitIntoNewZone()
fadeOut(function()
local cachedPart = inPart.part;
local cachedSong = inPart.song;
local canGoToNext = true;
local function playHandleAudio()
if canGoToNext then
if typeof(cachedSong) == "table" then
local onSong = 0;
sound.Looped=false;
for a,b in pairs (cachedSong) do
if typeof(b) == "number" then
sound.SoundId = "rbxassetid://"..b;
sound.TimePosition=0;
sound.Volume = 0;
sound:Play();
fadeIn();
sound.Ended:Wait()
sound:Stop()
end
end
playHandleAudio()
elseif typeof(cachedSong) == "number" then
sound.Looped=true;
sound.SoundId = "rbxassetid://"..cachedSong;
sound.Volume = 0;
sound:Play();
fadeIn();
else
error("Invalid song tpye, must be type of int, or object.")
end
end
end
if sound.IsPlaying then
fadeOut(function()
playHandleAudio()
end);
else
playHandleAudio();
end
end);
return function()
canGoToNext=false;
sound.Looped=false;
fadeOut(function()
sound:Stop();
end)
end
end
for a,b in pairs (partsWithAudio) do
spawn(function()
local part = b.part;
local minReigon = part.Position - (0.5 * part.Size)
local maxReigon = part.Position + (0.5 * part.Size)
local reigon = Region3.new(minReigon, maxReigon);
local isIn = false;
local fired = false;
while wait(1/5) do
local parts=workspace:FindPartsInRegion3(reigon);
local inPartLoop = nil;
for c,d in pairs (parts) do
if d.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(d.Parent);
if player == game.Players.LocalPlayer then
inPartLoop = b;
end
end
end
if inPartLoop == nil then
if fired then
fired=false;
if cachedLastPart then
if cachedLastPart.part == b.part == false then
else
inPart=nil;
cachedLastPart=nil;
if context then
spawn(context);
else
spawn(exit);
end
end
else
inPart=nil;
if context then
spawn(context);
else
spawn(exit);
end
end
end
else
if fired == false then
fired=true;
inPart=b;
cachedLastPart=b;
if cachedLastPart then
if cachedLastPart.part == inPartLoop.part == false then
spawn(function()
context=exitIntoNewZone();
end)
else
spawn(function()
context=enter();
end)
end
else
spawn(function()
context=enter();
end)
end
end
end
end
end)
end
That’s not an error… uhh are you sure it’s not working. Try checking your output/console. I tested this code right before I sent it here. Maybe you’re volume isn’t up – or the soundIds are not right???
whoops just noticed, and no i dont see anything in output. what should the settings look like for my sound?
The script is working fine for me, I did not need to change any settings for the Sound.
Confirm your script looks like this?
local partsWithAudio = {
{
part = workspace.SoundRegions.Sound1,
song = 6304093764
},{
part = workspace.SoundRegions.Sound2,
song = 332006582
},{
part = workspace.SoundRegions.Sound3,
song = 0
},
};
local inPart = nil;
local sound = script.Sound;
local cachedLastPart = nil;
local context = nil;
function fadeIn()
for i=sound.Volume, 1, (1-sound.Volume)/10 do
sound.Volume = i;
wait(1/10)
end
end
function fadeOut(done)
for i=sound.Volume, 0, -sound.Volume/10 do
sound.Volume = i;
wait(1/10)
end
if done then
done();
end
end
function enter()
if inPart then
local cachedPart = inPart.part;
local cachedSong = inPart.song;
local canGoToNext = true;
local function playHandleAudio()
if canGoToNext then
if typeof(cachedSong) == "table" then
local onSong = 0;
sound.Looped=false;
for a,b in pairs (cachedSong) do
if typeof(b) == "number" then
sound.SoundId = "rbxassetid://"..b;
sound.TimePosition=0;
sound.Volume = 0;
sound:Play();
fadeIn();
sound.Ended:Wait()
sound:Stop()
end
end
playHandleAudio()
elseif typeof(cachedSong) == "number" then
sound.Looped=true;
sound.SoundId = "rbxassetid://"..cachedSong;
sound.Volume = 0;
sound:Play();
fadeIn();
else
error("Invalid song tpye, must be type of int, or object.")
end
end
end
if sound.IsPlaying then
fadeOut(function()
playHandleAudio()
end);
else
playHandleAudio();
end
return function()
canGoToNext=false;
sound.Looped=false;
fadeOut(function()
sound:Stop();
end)
end
end
end
function exit()
if inPart then
fadeOut(function()
sound.SoundId = 0;
sound:Stop();
end)
inPart = nil;
end
end
function exitIntoNewZone()
fadeOut(function()
local cachedPart = inPart.part;
local cachedSong = inPart.song;
local canGoToNext = true;
local function playHandleAudio()
if canGoToNext then
if typeof(cachedSong) == "table" then
local onSong = 0;
sound.Looped=false;
for a,b in pairs (cachedSong) do
if typeof(b) == "number" then
sound.SoundId = "rbxassetid://"..b;
sound.TimePosition=0;
sound.Volume = 0;
sound:Play();
fadeIn();
sound.Ended:Wait()
sound:Stop()
end
end
playHandleAudio()
elseif typeof(cachedSong) == "number" then
sound.Looped=true;
sound.SoundId = "rbxassetid://"..cachedSong;
sound.Volume = 0;
sound:Play();
fadeIn();
else
error("Invalid song tpye, must be type of int, or object.")
end
end
end
if sound.IsPlaying then
fadeOut(function()
playHandleAudio()
end);
else
playHandleAudio();
end
end);
return function()
canGoToNext=false;
sound.Looped=false;
fadeOut(function()
sound:Stop();
end)
end
end
for a,b in pairs (partsWithAudio) do
spawn(function()
local part = b.part;
local minReigon = part.Position - (0.5 * part.Size)
local maxReigon = part.Position + (0.5 * part.Size)
local reigon = Region3.new(minReigon, maxReigon);
local isIn = false;
local fired = false;
while wait(1/5) do
local parts=workspace:FindPartsInRegion3(reigon);
local inPartLoop = nil;
for c,d in pairs (parts) do
if d.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(d.Parent);
if player == game.Players.LocalPlayer then
inPartLoop = b;
end
end
end
if inPartLoop == nil then
if fired then
fired=false;
if cachedLastPart then
if cachedLastPart.part == b.part == false then
else
inPart=nil;
cachedLastPart=nil;
if context then
spawn(context);
else
spawn(exit);
end
end
else
inPart=nil;
if context then
spawn(context);
else
spawn(exit);
end
end
end
else
if fired == false then
fired=true;
inPart=b;
cachedLastPart=b;
if cachedLastPart then
if cachedLastPart.part == inPartLoop.part == false then
spawn(function()
context=exitIntoNewZone();
end)
else
spawn(function()
context=enter();
end)
end
else
spawn(function()
context=enter();
end)
end
end
end
end
end)
end
still does not work, can you screenshot the placement of everything in workspace just to confirm that i have everything in the right place?
Not really, I’ve closed studio. Can you confirm that these Reigon parts are sized so the player can fit into them, with plenty of vertical margin [incase they jump] and are big enough for the player to walk around in? Also, since you have Sound3 set to sound 0, make sure you’re not walking in it [as you’ve set it to play nothing.
i actually already had another region script but it didn’t fade, and that one seemed to work, i just didnt know how to make it fade when the player walks away. (its disabled)
local soundRegions = workspace:WaitForChild("SoundRegions")
soundRegions = soundRegions:GetChildren()
local soundManagement = {}
for _,region in pairs(soundRegions) do
local info = {}
local region3 = Region3.new(region.Position-(region.Size/2),region.Position+(region.Size/2))
region.Transparency = 1
info.Region = region3
info.Sound = script.SoundRegions:FindFirstChild(region.name).Sound
table.insert(soundManagement,info)
end
game:GetService("RunService").RenderStepped:Connect(function()
for _,soundInfo in pairs(soundManagement) do
local region = soundInfo.Region
local sound = soundInfo.Sound
local parts = workspace:FindPartsInRegion3WithWhiteList(region,plr.Character:GetDescendants())
if #parts > 0 then
if not sound.IsPlaying then
sound:Play()
end
else
sound:Stop()
end
end
end)
it had the sounds in folders under the script
heres mine
So if you don’t have a zone three, remove it from the array… like this
local partsWithAudio = {
{
part = workspace.SoundRegions.Sound1,
song = 6304093764
},{
part = workspace.SoundRegions.Sound2,
song = 332006582
}
};
alright replaced it, still does not work though.
Do you have a link to the game?