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

This sound playing script in a part is not working?

Asked by 7 years ago
Edited 7 years ago

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.

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

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

0
First off, I thank you for your well thought out and detailed reply, I am tremendously grateful. Unfortunately, when entering the part, I still lag. Strangely enough, this happens ONLY on my custom character model, and not on a normal roblox model. Also, when on the regular roblox model, upon leaving the part, the music does not stop playing, and once i re-enter the part, it starts over. Cavedudemann 2 — 7y
0
Oh hang on, I forgot. Debounce. Also, I had lines 3 and 4 the wrong way round. Flip those around, and give this page a look. http://wiki.roblox.com/?title=Debounce SquirreIOnToast 309 — 7y
0
So, would I just add debounce like this?: script.Parent.Touched:connect(debounce(function(hit) Cavedudemann 2 — 7y
0
I read that page quite a few times, and I am still confused on how to apply it to this situation. Cavedudemann 2 — 7y
View all comments (4 more)
0
Just wanted to thank you again for your answer: thanks. If you reply to these comments I might just have to fall down at your feet and cry. This issue has been plaguing me since I started making this game, and it would be absolutely amazing to get it fixed. :D Cavedudemann 2 — 7y
0
It's a bit hard to fit in a comment; edited the main post SquirreIOnToast 309 — 7y
0
Forgive me for my noobness, but I tried to add debounce and now the sound won't play at all! It would seem to me if debounce = true before the function begins it would do nothing right? I'm really sorry to bother you again. :/ Cavedudemann 2 — 7y
0
Problem lies with you not setting debounce to false at the end of your function. SquirreIOnToast 309 — 7y
Ad

Answer this question