So I bassically made two scripts, one that my goal was to make the background music of the game (SONIC) play when you leave an area were (HALO) is playing. Bassically, I put a brick that changes the music once you go into a house into HALO, and then I made a brick that changes the music back to SONIC when you step out of the house. Here's both scripts.
function onTouch(hit) if hit.Parent:FindFirstChild("Humanoid") then if game.Workspace.TV.Script.HALO.IsPlaying == true then game.Workspace.TV.Script.HALO:Stop() wait(1) game.Workspace.SONIC:Play() end end end game.Workspace.MusicPlay.Touched:connect(onTouch)
function onTouch(hit) if hit.Parent:FindFirstChild("Humanoid") then if game.Workspace.SONIC.IsPlaying == true then game.Workspace.SONIC:Stop() wait(1) game.Workspace.TV.Script.HALO:Play() end end end game.Workspace.MusicPlay.Touched:connect(onTouch)
My problem is when I repededly walk over the blocks, the songs get messed up and start to randomly change eachother. I just want a song to play in a certain area, and then stop playing once out of that area. How would I be able to do that?
http://wiki.roblox.com/index.php?title=Sounds#3D_Sound <- GOTO: 3D sound
You could either use 3D Sound
in your place but this may not be very effective if you have other sounds that need to be played for a much further distance. To solve your problem you could just make it so that if the player's Magnitude
is greater than a specific value then stop your sound. To make it sound more professional you can make your own 'Fade' function by using a for loop. Another way to find the distance between an object and player is to use Raycasting
.
Here are the ROBLOX Wiki links for Raycasting/Magnitude:
Also please note that you will need a part where the magnitude is subtracted from.
Hope this helped! ThePhantomDeveloper