I've been commissioned to add some ambient sounds to a game for my group. They want specific sounds to play for certain areas in the building (ie: cutlery clinking and backround chatter for the dining room, cheering for the sports area, etc).
I know I probably want to insert the sound play function combined with an on.touch(hit), but that would only play the ambient noise until it stops, and I only want it to play while I am touching that block. I am unsure if a simple while loop would work for this, as that might repeatedly play the start of the sound. This is one of the two major issues for the code, the second being fade-outs. It is a requirement that the sound gradually fades in and out when you start and stop touching the block, and I have absolutely no idea how to do this. From a general standpoint, I would like this to happen when a player touches the block.
While Block touched | | \/ Sound Fade In and then continuously loop. | | \/ Block is no longer being touched. | | \/ Sound Fade Out and then stop.
Obviously, in order for this to work, the sound would have to only play for the player who touched the block.
Thank you in advance, Akumokagetsu
The way I'd do it is using a while loop to continuously check the distance between the player and the blocks that emits the sound, and make the sound volume proportional to that distance. Like this :
MySound:Play() MaxDistance = 100 while wait(1) do local Distance = (game.Players.LocalPlayer.Character.Torso.Position - game.Workspace.SoundEmitter.Position).magnitude MySound.Volume = math.max(0, 1 - Distance/MaxDistance) end