Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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.

1 comment

0

If none of these configurations are fixing your issue, it may be a problem with your mathematics.

This discussion is preserved as it was. New answers, comments, and votes are closed.