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.
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.
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.
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.
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.
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.1local SoundService = game:GetService("SoundService")2local backgroundMusic = SoundService.BackgroundMusic3
Sounds are played using the Play function. In a new line, call it on the backgroundMusic variable.1local SoundService = game:GetService("SoundService")2local backgroundMusic = SoundService.BackgroundMusic34backgroundMusic:Play()5
Test the game and confirm that the music is audible.
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.