Audio in Roblox is created with a Sound object. Sounds can be positional, such as the sound of a waterfall, or universal for all players. This tutorial will show you how to create a universal sound to play background music.
Finding Music
Music can be uploaded, or it can be obtained from the Toolbox which contains thousands of free-to-use tracks. For this tutorial, you'll need the asset ID of an uploaded track or one found in the marketplace.
Uploading Music
On Roblox, uploading audio files comes at a small cost in Robux — this accounts for the time it takes moderators to review every sound file that users upload.
Visit the Create Audio page to upload a track.
Once the file is uploaded, it will appear in the list on the Audio page. Click the name to open its dedicated page and copy its numeric ID from the URL in the browser window.
Audio Marketplace
A wide variety of songs can also be found in the marketplace.
Open the Toolbox and go to the Marketplace tab. From the dropdown menu, select Audio.
Click the Sort button and then, in the Creator field, type in a contributor such as Roblox or Monstercat (electronic music label partnered with Roblox).
While browsing, sample songs by pressing the preview button.
After finding a song, right-click the listing and select Copy Asset ID. This ID will be used later to add in the background music.
Playing Music
With a previously copied asset ID, a new Sound object can be created. Once set up, a script will play the music for each player.
Sound Setup
If a sound object is parented to a part, sound will emit from its position. If a sound object is parented to SoundService, it will play at the same volume at every point in the game world. This makes SoundService ideal for storing background music.
In SoundService, insert a Sound object named BackgroundMusic.
In the newly created sound, find the SoundId property. Paste in the previously copied sound ID (or use one below) and press Enter.
Test that the sound works by clicking the Preview button.
Here are some sample music IDs you can use:
- Creepy Organ/Dungeon - rbxassetid://1843463175
- Upbeat Electronica - rbxassetid://1837849285
- Grandiose Fantasy - rbxassetid://1848183670
Playing the Song
Background music can be played in a game through a script.
In StarterPlayer > StarterPlayerScripts, create a LocalScript named MusicPlayer.
In the script, create variables to store SoundService and the BackgroundMusic object.
local SoundService = game:GetService("SoundService")local backgroundMusic = SoundService.BackgroundMusicSounds are played using the Play function. In a new line, call it on the backgroundMusic variable.
local SoundService = game:GetService("SoundService")local backgroundMusic = SoundService.BackgroundMusicbackgroundMusic:Play()Test the game and confirm that the music is audible.
Audio Properties
Currently, the music doesn't loop. Additionally, the original sound file may be too loud for background music. These settings can be changed through two properties.
In the BackgroundMusic properties, toggle Looped to be on.
Lower the Volume to around 0.25.
With this project done, explore using scripts to implement other features in music. For instance, try using a script to shuffle songs in a soundtrack or play songs in different areas of your game world.
For more on sounds and background music, see Audio.