I asked this question a couple days ago, and I received one answer, but it did not work. I am going to try re-asking this question a little differently this time, to make sure I'm clear. So, I have a part, I made two scripts within the part and then wrote this in the first:
script.Parent.Touched:connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then game.Players[hit.Parent.Name].PlayerGui.Sound:Resume() end end)
And this in the second:
script.Parent.TouchEnded:connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then game.Players[hit.Parent.Name].PlayerGui.Sound:Pause() end end)
Obviously, this is not working. The answer I got suggested I use IsPlaying, so I wrote another script, which is this:
script.Parent.Touched:connect(function(hit) if game.StarterGui.Sound.IsPlaying == false then game.StarterGui.Sound:Resume() end end)
I don't know how to connect a statement for finding the humanoid and then use the IsPlaying statement. Anyways, every time I step within the part, it causes ridiculous lag, so I think the script must be running constantly, though I'm not sure. I want to just walk into the part, the sound plays, I walk out, the sound stops. I also would like it not to lag me ridiculously bad. Any advice or suggestions would be greatly appreciated. Thanks!
****EDIT: @SquirrelOnToast Figured out how to do it. Thanks for the help, but your suggestion ultimately did not work. I am very appreciative though of the time you took to help me out, it started me on the right trail, and ultimately it turned out to be very simple. I will mark your answer as accepted, though.
Okay, there are a few things wrong here, and some nitpicking of my own to do. First, my nitpicking, because I cannot bear to write a solution without making it proper to my own standard:
Your functions can both be in the same script. In fact, it is better that they are. We can mash your two pieces of code together like so:
script.Parent.Touched:connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then game.Players[hit.Parent.Name].PlayerGui.Sound:Resume() script.Parent.TouchEnded:connect(function (hitEnded) if hitEnded.Parent:FindFirstChild("Humanoid") then game.Players[hit.Parent.Name].PlayerGui.Sound:Pause() end end) end end)
This is better as :TouchEnded()
is not being triggered constantly: only after it is needed. However, we are not done yet; there is a fix to be made!
script.Parent.Touched:connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then game.Players[hit.Parent.Name].PlayerGui.Sound:Play() -- This is BETTER than resume, because if you touch the part for shorter time than the sound plays, it will resume where it left off. script.Parent.TouchEnded:connect(function (hitEnded) if hitEnded.Parent:FindFirstChild("Humanoid") then game.Players[hit.Parent.Name].PlayerGui.Sound:Stop() -- Better for the reason listed above. end end) end end)
There is one last thing however; if you attempt to run this, your sound will not play because it needs to be preloaded. The easiest way to do this is to simply set the soundId during your script, as ROBLOX will preload a sound if it is assigned its SoundId during running.
game.Players[hit.Parent.Name].PlayerGui.Sound.SoundId = "" -- Simply copy and paste the ENTIRE link from the sound's soundId into the quotes and it will work.
Although it is not necessary to load for every single player, I have no idea where you store the GUI beforehand, so I just use that script to do it JUST before the sound is needed. A combination of the improved script, and the preloader, looks like this:
script.Parent.Touched:connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then game.Players[hit.Parent.Name].PlayerGui.Sound:Play() -- This is BETTER than resume, because if you touch the part for shorter time than the sound plays, it will resume where it left off. game.Players[hit.Parent.Name].PlayerGui.Sound.SoundId = "" script.Parent.TouchEnded:connect(function (hitEnded) if hitEnded.Parent:FindFirstChild("Humanoid") then game.Players[hit.Parent.Name].PlayerGui.Sound:Stop() -- Better for the reason listed above. end end) end end)
Reply if you need extra help, you're welcome.
EDIT How to apply debounce:
debounce = false function new() if debounce == true then return end debounce = true --Insert function here debounce = false end
This way, it stops the function from calling twice. ROBLOX hitting events are very temperamental and can trigger several times from one touch. This should fix it if implemented correctly