In-Game Sounds

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.

Positional Sounds

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.

Create a Sound

  1. In any desired part, create a new Sound object named WaterfallSound.

  2. In the properties, find SoundId and change it to this waterfall ambience: rbxassetid://6564308795.

    alt

  3. For continuous playback when the game starts, toggle Playing and Looped to be on.

    alt

  4. Test the game to confirm you hear the waterfall ambience.

Adjust Sound Distance

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.

  1. Change the RollOffMaxDistance to 30. This property is measured in studs.

    alt

  2. For a smoother fade, change the RollOffMode to InverseTapered. This makes approaching the sound feel less sudden.

    alt

  3. Run the project. Notice how sound is only heard near the object.

Fine Tuning the Roll Off

Depending on your needs, you may want to adjust different properties for special effects or increased realism. See the following properties:

  • RollOffMinDistance - Minimum distance (in studs) a sound decreases in volume.
  • SoundGroup - Used to adjust and balance volume between groups of sounds, like background music and in-game effects.

Feedback Sounds

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.

Set Up Collectables

The remainder of this tutorial uses a pre-made model. This model includes parts and scripts so players can collect gemstones.

  1. In a browser, open the Collectable Gems Model page, and click the Get button.

    alt

  • 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.
  1. In SoundService, create a new Sound named FeedbackSound.

    alt

  2. In FeedbackSound, set the SoundId to rbxassetid://4110925712- the SoundId of the simple chime downloaded from the model page.

    alt

Setup the Script

  1. In StarterPlayer > StarterPlayerScripts, create a new local script named CollectableSounds.

  2. The code below will run the partTouched function whenever the player touches a collectable. Copy the code into your script.


    1local pickupObjects = game.Workspace.Collectables.Objects
    2local objectsArray = pickupObjects:GetChildren()
    3
    4local function partTouched(otherPart, objectPart)
    5 local whichCharacter = otherPart.Parent
    6 local humanoid = whichCharacter:FindFirstChildWhichIsA("Humanoid")
    7
    8 if humanoid and objectPart.CanCollide == true then
    9
    10 end
    11end
    12
    13-- Binds every object part to the touch function so it works on all parts
    14for objectIndex = 1, #objectsArray do
    15 local objectPart = objectsArray[objectIndex]
    16 objectPart.Touched:Connect(function(otherPart)
    17 partTouched(otherPart, objectPart)
    18 end)
    19end
    20

Play a Sound

  1. Create a variable for SoundService, then another variable to store the feedback sound.


    1local pickupObjects = game.Workspace.Collectables.Objects
    2local objectsArray = pickupObjects:GetChildren()
    3
    4local SoundService = game:GetService("SoundService")
    5local feedbackSound = SoundService:FindFirstChild("FeedbackSound")
    6
    7local function partTouched(otherPart, objectPart)
    8
  2. To play the chime, find the function partTouched. Within the if statement, call feedbackSound:Play() to play the sound.


    1local function partTouched(otherPart, objectPart)
    2 local whichCharacter = otherPart.Parent
    3 local humanoid = whichCharacter:FindFirstChildWhichIsA("Humanoid")
    4
    5 -- Play the sound, once finished, destroy the object
    6 if humanoid and objectPart.CanCollide == true then
    7 feedbackSound:Play()
    8 end
    9end
    10
    11
  3. Test the game to confirm that when the player touches a collectable, it both disappears and plays a sound.