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

How would I have this sound play once when you are in range, then stop when you are out of range?

Asked by 4 years ago

I'm trying to make for my door script, where when you come close to the door a sound plays, and a GUI pops up. I got everything working, however I cannot get this to work.

I’ve noticed the issue is mainly that its probably playing then stopping immediately

I haven’t tried much solutions except adding a debounce or some other things.

Here is the local script, (I only want the sound to play for the client, that’s the reason.):

local mag2 = (Character.HumanoidRootPart.Position - Door.Center.Position).magnitude
    if mag2 <= Door.Config.Range.Value then
        if not played then
        sound:Play()
        wait(0.1)
        played = true
    if played == true then
        sound:Stop()
        played = false
        end
        end
    end 

1 answer

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

The if statement is fired immediately after played is set to true, in conclusion, your hypothesis is slightly correct, the soundtrack is stopped how it was supposed to be in respect to the program, however, it's stopped too quickly, that is your issue. If you wish to have the soundtrack stopped when it's complete, try using the isPlaying signal instead, or you can simply just call Play and let it stop on it's own:

local Sound = workspace.Sound
-- Conditionals for playing the sound.
sound:Play()

if not (Sound.isPlaying) then
    sound.Stop()
end

If you're wanting to cut the soundtrack quickly, to play a certain piece of the track, it may be better to try adjusting the yielding value.

Just another tip

This may also be a cause of your problem. A Sound instance is required to be placed within the workspace to functionally operate; if the instance is elsewhere, it won't play. If this is the cause, adjusting the location will not affect your desire to play it locally.

0
If none of these configurations are fixing your issue, it may be a problem with your mathematics. Ziffixture 6913 — 4y
Ad

Answer this question