Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

How do I add a sound to an object?

Asked by 9 years ago

I didn't want to make the question too big, but I also want to know how to make it change volume when you move closer or back away from the object playing the sound.

ex: http://www.roblox.com/games/148634071/Ocean-Drive

Just to inform, I'm an early learner in script, so I don't know much about how things work. It'd also be appreciated if there is any help that if you could give me a description on how it all works (not saying you have to, but it'd help a lot!)

Thanks!

0
Just put the 'Sound' type instance in a 'BasePart' type instance, and it'll play from your distance from the 'BasePart', if that's what your asking specificly. :) TheeDeathCaster 2368 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

Place the Sound object in the object, and then do Sound:Play().

For example...

local part = workspace.YourPart -- reference the Part from the Workspace
local sound = game.ReplicatedStorage.YourSound:Clone() -- get the Sound from somewhere, in this case, ReplicatedStorage, and clone it
sound.Parent = part -- move the Sound to the Workspace

function playSound()
    sound:Play() -- play the sound
end

part.Touched:connect(playSound) -- when the Part's Touched event is called, execute playSound()

More information can be found regarding sounds on this wiki article.

0
There is a problem with your code; The function, and connection line: What if an object were to Touch the 'BasePart' type instance? The function would fire multiple times, but, as well as running multiple Audios at once, and if it's looped, and it fired multiple times, doing the ':Stop()' method will not stop it. TheeDeathCaster 2368 — 9y
0
I got to put the script in and enter all the parts needed, but it still doesn't after multiple times. I don't know what I'm doing wrong, but I feel as if I totally screwed up. What really gets me off track is the ReplicatedStorage part since I don't know what it does and how to use it. Also the Touched:connect is confusing and I didn't include it in the script (I didn't want to stress out to put j ownedim 0 — 9y
0
I was only trying to show an example, not make a script that should be used practically. Programmix 285 — 9y
Ad

Answer this question