Description
A basic juke box to play music in the editor and game in 2D.
Features
- Volume
- Length
- Fade
- Random Tracks played
Steps
-
Drop and drag “Basic Juke Box” template on the world tree
-
Set the volume and length as desired.
-
Fade of 3 seems to work well, but can be changed also.
-
Add one or more tracks to play by adding to the array.
-
To play in the editor, simulate (check) on the locator.
BasicJukeBoxScript
local BasicJukeBoxScript = {}
BasicJukeBoxScript.Properties = {
{ name = "volume", type = "number", min = 0, max = 2, default = 1, editor = "slider", },
{ name = "trackLength", type = "number", default = 180, },
{ name = "fadeInOut", type = "number", default = 3, },
{ name = "tracks", type = "soundasset", container = "array", },
}
function BasicJukeBoxScript:ClientInit()
self:Schedule(function()
while true do
local track = math.random(#self.properties.tracks)
print("Now Playing", "track", track, self.properties.tracks[track]:GetName())
self.jukeBox = self:GetEntity():
PlaySound2D(self.properties.tracks[track], self.properties.fadeInOut)
self.jukeBox:SetVolume(self.properties.volume)
Wait(self.properties.trackLength)
self.jukeBox:Stop(self.properties.fadeInOut)
end
end)
end
return BasicJukeBoxScript
Note
I just wanted a simple music player in the editor and 2D for the game. This could be expanded upon but I thought i’d share a basic working model to use as is or build upon.