title | description | prev |
---|---|---|
In-Game Sounds |
The process for creating positional and feedback sounds to enhance an experience. |
/tutorials/building/environments/playing-background-music |
In addition to background music, in-game audio can enhance a player's experience. This tutorial will cover two forms of in-game sounds: positional and feedback sounds.
For the first example, you'll create a positional sound for a waterfall. In the second example, a script will be used to play a jingle when players touch a collectable.
When a Sound object is parented to a part or attachment, it becomes positional. Audio will emit from its location and grow louder as players get closer, as in the case of this waterfall.
-
In any desired part, create a new Sound object named WaterfallSound.
-
In the properties, find SoundId and change it to this waterfall ambience:
Custom sounds can be imported using the [Asset Manager](../../../projects/assets/manager.md). Additionally, free sounds uploaded by Roblox and the community can be found using the [Toolbox](../../../projects/assets/toolbox.md).rbxassetid://6564308795
. -
For continuous playback when the game starts, toggle Playing and Looped to be on.
-
Test the game to confirm you hear the waterfall ambience.
Notice when testing, the audio plays immediately, even if the player is far away from the object. Using the roll-off properties, you can modify the distance at which a player hears a sound to create fading effects.
-
Change the
Class.Sound.RollOffMaxDistance|RollOffMaxDistance
to 30. This property is measured in studs. -
For a smoother fade, change the RollOffMode to InverseTapered. This makes approaching the sound feel less sudden.
-
Run the project. Notice how sound is only heard near the object.
Depending on your needs, you may want to adjust different properties for special effects or increased realism. See the following properties:
Class.Sound.RollOffMaxDistance|RollOffMinDistance
- Minimum distance (in studs) a sound decreases in volume.Class.SoundGroup
- Used to adjust and balance volume between groups of sounds, like background music and in-game effects.
Sounds can be played on command using scripts. You can link sounds to events, such as players touching a part or interacting with a menu. Here, you'll create a script that plays a chime whenever players touch collectable objects.
The remainder of this tutorial uses a pre-made model. This model includes parts and scripts so players can collect gemstones.
-
In a browser, open the Collectable Gems Model page, and click the Get button.
- In Studio, go to the View tab and click on the Toolbox.
- In the Toolbox window, click on the Inventory button. Then, make sure the dropdown is on My Models.
- Select the Collectable Gems model to add it into the game.
-
In SoundService, create a new Sound named FeedbackSound.
-
In FeedbackSound, set the SoundId to
rbxassetid://4110925712
- the SoundId of the simple chime downloaded from the model page.
-
In StarterPlayer > StarterPlayerScripts, create a new local script named CollectableSounds.
-
The code below will run the
partTouched
function whenever the player touches a collectable. Copy the code into your script.local pickupObjects = workspace.Collectables.Objects local objectsArray = pickupObjects:GetChildren() local function partTouched(otherPart, objectPart) local whichCharacter = otherPart.Parent local humanoid = whichCharacter:FindFirstChildWhichIsA("Humanoid") if humanoid and objectPart.CanCollide == true then end end -- Binds every object part to the touch function so it works on all parts for objectIndex = 1, #objectsArray do local objectPart = objectsArray[objectIndex] objectPart.Touched:Connect(function(otherPart) partTouched(otherPart, objectPart) end) end
-
Create a variable for SoundService, then another variable to store the feedback sound.
local pickupObjects = workspace.Collectables.Objects local objectsArray = pickupObjects:GetChildren() local SoundService = game:GetService("SoundService") local feedbackSound = SoundService:FindFirstChild("FeedbackSound") local function partTouched(otherPart, objectPart)
-
To play the chime, find the function
partTouched
. Within the if statement, callfeedbackSound:Play()
to play the sound.local function partTouched(otherPart, objectPart) local whichCharacter = otherPart.Parent local humanoid = whichCharacter:FindFirstChildWhichIsA("Humanoid") -- Play the sound, once finished, destroy the object if humanoid and objectPart.CanCollide == true then feedbackSound:Play() end end
-
Test the game to confirm that when the player touches a collectable, it both disappears and plays a sound.