48 lines
1.8 KiB
Luau
48 lines
1.8 KiB
Luau
-- BackgroundMusic (LocalScript, StarterPlayerScripts): quiet background music.
|
|
-- Track: SoundService.Music["Happy Adventure"] (~71 s). It plays flat (not 3D), quietly, fades in and out, and after
|
|
-- every play there is a pause of PAUSE seconds of silence, so a short track does not loop non-stop and annoy.
|
|
-- Starts after the intro shot (IntroDone) so the camera fly-over stays calm.
|
|
local SoundService = game:GetService("SoundService")
|
|
local TweenService = game:GetService("TweenService")
|
|
local Players = game:GetService("Players")
|
|
|
|
local VOLUME = 0.12 -- the music volume (0..1). Raise / lower here
|
|
local FADE = 4 -- seconds of fade in / fade out
|
|
local PAUSE = NumberRange.new(25, 50) -- seconds of silence between plays
|
|
|
|
local folder = SoundService:WaitForChild("Music", 10)
|
|
local template = folder and folder:FindFirstChild("Happy Adventure")
|
|
if not template then return end
|
|
|
|
local music = template:Clone()
|
|
music.Name = "BackgroundMusic"
|
|
music.Looped = false
|
|
music.Volume = 0
|
|
music.Parent = SoundService -- outside any part = flat sound, heard the same everywhere
|
|
|
|
-- wait for the intro shot (max 30 s)
|
|
local player = Players.LocalPlayer
|
|
local t0 = os.clock()
|
|
while player:GetAttribute("IntroDone") ~= true and os.clock() - t0 < 30 do task.wait(0.25) end
|
|
task.wait(1.5)
|
|
|
|
local function fade(to)
|
|
local tw = TweenService:Create(music, TweenInfo.new(FADE, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), { Volume = to })
|
|
tw:Play()
|
|
return tw
|
|
end
|
|
|
|
if not music.IsLoaded then music.Loaded:Wait() end
|
|
while music.Parent do
|
|
music.TimePosition = 0
|
|
music.Volume = 0
|
|
music:Play()
|
|
fade(VOLUME)
|
|
-- fade out shortly before the end of the track
|
|
local length = music.TimeLength
|
|
task.wait(math.max(0, length - FADE - 0.2))
|
|
fade(0).Completed:Wait()
|
|
music:Stop()
|
|
task.wait(math.random(PAUSE.Min, PAUSE.Max))
|
|
end
|