I'm trying to get music to play inside a particular area when touched.
Say, I have a room and I want the music to only play inside of that room.
So far, I've managed to get a brick to play music when touched, and put it on the entry to the room and am using the following script:
local sound = Instance.new("Sound", Workspace) sound.Pitch = 1 sound.SoundId = "rbxassetid://IDHERE" sound.Volume = 1 sound.Looped = true script.Parent.Touched:connect(function() sound:play() end)
The problem that I am having, is that the music is playing whenever the block is "touched" meaning that even just standing on it causes it to just play barely a second of the song before restarting- which is the least annoying problem, since I have put it at the entry to the room and it can be heard inside the room as the full song instead. Since atm, I have a small 1 stud thin strip on entry to the room that causes the music to be played.
The problem I do have, is how to stop the music when another sound playing brick is touched.
Or how to code a switch that will stop all music when pressed? I'm really new to code, and I'm pretty much useless at it, so if anyone could help me that'd be really helpful :)
Edit: I'm thinking I could have the "main part" before the room set to stop music when walked on? So that when they enter the different rooms the music would play, but on leaving would stop.
I think the sound.Looped = true
caused the problem, you should remove it.
If you want the music stop when player not touch the part, here is the script:
local sound = Instance.new("Sound", Workspace) sound.Pitch = 1 sound.SoundId = "rbxassetid://IDHERE" sound.Volume = 1 script.Parent.Touched:connect(function() sound:play() end) script.Parent.TouchEnded:Connect(function() sound:Stop() end)
That's all, hope i helped you :).