PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Apr 17 2019, 9:30 PM PST 10 min

While building a game, it’s easy to overlook the importance of sounds and music. Combined creatively, these can set an atmospheric or exciting tone, build suspense, and bring life to your game’s characters.

Uploading Custom Audio

On Roblox, uploading audio files comes at a small cost in Robux — this accounts for the time it takes us to review every sound file that users upload. We do this to ensure the safety and integrity of our users and Roblox in general.

To upload audio on the Roblox site:

  1. Visit the Create Audio page where you’ll be prompted to upload a local file and give it a name. Audio files must be .mp3 or .ogg format, shorter than 7 minutes, and smaller than 19.5 MB.

  2. Click the Estimate Price button. The cost to upload an audio file is based on the following pricing structure:

Length Cost
0–10 seconds 20
10–30 seconds 35
30 seconds – 2 minutes 70
2–7 minutes 350
  1. Click the Purchase button to upload the audio file.

  2. Once the file is uploaded, it will appear in the list on the page. Click the name to open its dedicated page and copy/record its numeric ID from the URL in the browser window. This ID will be required for playback testing in the sections below.

Playing Music

Playing background music in a game can be accomplished via scripting, as follows:

  1. In the Explorer window of Studio, add a new ModuleScript to ReplicatedStorage.
  1. Rename the new script AudioPlayer.
  1. In the script, delete all existing lines and paste in the following code:
local module = {}

-- Roblox services
local ContentProvider = game:GetService("ContentProvider")

-- Function to preload audio assets
module.preloadAudio = function(assetArray)
	local audioAssets = {}

	-- Add new "Sound" assets to "audioAssets" array
	for name, audioID in pairs(assetArray) do
		local audioInstance = Instance.new("Sound")
		audioInstance.SoundId = "rbxassetid://" .. audioID
		audioInstance.Name = name
		audioInstance.Parent = game.Workspace
		table.insert(audioAssets, audioInstance)
	end

	local success, assets = pcall(function()
		ContentProvider:PreloadAsync(audioAssets)
	end)
end

-- Function to play an audio asset
module.playAudio = function(assetName)
	local audio = game.Workspace:FindFirstChild(assetName)
	if not audio then
		warn("Could not find audio asset: " .. assetName)
		return
	end
	if not audio.IsLoaded then
		audio.Loaded:wait()
	end
	audio:Play()
end

return module
  1. Now create a new LocalScript inside StarterPlayerScripts (a child of StarterPlayer).
  1. In the script, delete all existing lines and paste in the following code. If desired, input different track names and audio IDs starting on line 9 (see Uploading Custom Audio above).
-- Roblox services
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Require module
local AudioPlayerModule = require(ReplicatedStorage:WaitForChild("AudioPlayer"))

-- Preload music tracks
AudioPlayerModule.preloadAudio({
	["Lucid_Dream"] = 1837103530,
	["Desert_Sands"] = 1848350335
})

-- Play music track
AudioPlayerModule.playAudio("Lucid_Dream")
  1. Playtest the game and you should hear the track name specified on line 14 playing in the background.
-- Play music track
AudioPlayerModule.playAudio("Lucid_Dream")

Playing Sounds

Unlike background music, sounds will often originate from a specific place or object inside the game — the engine of a vehicle, the battle cry of an enemy, etc. However, they may also be played as an “ambient” sound like wind blowing or rain falling.

3D Sounds

A sound placed in a BasePart or an Attachment will emit its sound from the part’s BasePart/Position|Position or the attachment’s Attachment/WorldPosition|WorldPosition. These are considered 3D sounds and they share the following traits:

  • Volume — A 3D sound will play louder as a player gets closer to its source.
  • Stereo — If one side of a player is closer to the sound’s source, the player’s corresponding speaker will be louder.
  • Doppler Effect — If the source of a 3D sound is approaching a player, it will play at a higher pitch. Similarly, it will play at a lower pitch if the source is traveling away from the player.

If the source part/attachment already exists in a place, you can add a 3D sound directly within Studio:

  1. In the Explorer window, insert a new Sound object as a child of the source object.
  1. Select the new sound object and, in the Properties window, locate its SoundId property. Enter rbxassetid:// followed by a valid audio ID gathered in the Uploading Custom Audio section above, for instance rbxassetid://1847352423.
  1. Toggle its Playing property on (sounds do not play automatically, so you must explicitly tell them to start playing).
  1. Playtest the game and observe the 3D sound’s qualities based on your character’s proximity to the source.

Ambient Sounds

Ambient environmental sounds like wind and thunder don’t need to be attached to a part because they come from all around the player. As such, these type of sounds can be played using the same scripts shown in Playing Music above.

GUI Sounds

Sound effects for interactive GUI objects can be played using the same ModuleScript shown in Playing Music above. Instead of playing a sound immediately, however, you can hook it up to the GuiButton/Activated|Activated event listener for the button.

  1. In StarterGui, add a new ScreenGui, then create a new TextButton within it. If you’re unfamiliar with buttons and their basic options, see articles/Creating GUI Buttons|Creating GUI Buttons.

  2. Select the new button and insert a LocalScript.

  1. In the script, delete all existing lines and paste in the following code. If desired, input a different sound name and audio ID on line 9.
-- Roblox services
local ReplicatedStorage= game:GetService("ReplicatedStorage")

-- Require module
local AudioPlayerModule = require(ReplicatedStorage:WaitForChild("AudioPlayer"))

-- Preload sounds
AudioPlayerModule.preloadAudio({
	["Simple_Click"] = 3061551819
})

-- Reference to button
local textButton = script.Parent

-- Function activated when button is pressed
local function onButtonActivated()
	AudioPlayerModule.playAudio("Simple_Click")
end
textButton.Activated:Connect(onButtonActivated)
  1. Playtest the game and you should hear the sound name specified on line 17 play when the button is activated.
-- Function activated when button is pressed
local function onButtonActivated()
	AudioPlayerModule.playAudio("Simple_Click")
end
textButton.Activated:Connect(onButtonActivated)
Tags:
  • sound
  • music
  • audio
  • effects