How to make the sound currently playing fade out once stepped on a block?
So I've made a script that looks like this
local SoundID; local Volume;
local SoundID = 0 ; local Volume = 1;
local function CreateSound(soundID, volume, parent) local sound = Instance.new("Sound", parent); sound.Volume = 0; sound.Name = "_PlayOnTouchedSound"; sound.SoundId = "rbxassetid://".. soundID; sound.Looped = true;
sound:Play() repeat sound.Volume = sound.Volume + .1; wait(.1); until sound.Volume >= volume;
end;
script.Parent.Touched:connect(function(other) if other and game.Players:GetPlayerFromCharacter(other.Parent) then local player = game.Players:GetPlayerFromCharacter(other.Parent); local PlayerGui = player.PlayerGui
if not PlayerGui:FindFirstChild("_PlayOnTouchedSound") then CreateSound(SoundID, Volume, player.PlayerGui); elseif PlayerGui:FindFirstChild("_PlayOnTouchedSound") and PlayerGui:FindFirstChild("_PlayOnTouchedSound").SoundId ~= "rbxassetid://"..SoundID then PlayerGui:FindFirstChild("_PlayOnTouchedSound"):Remove() CreateSound(SoundID, Volume, player.PlayerGui); end; end;
end);
How can I make that once stepped on a special block, the sound starts to fade out and it takes at least 3 seconds for the sound to completely stop?