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

How to get sound to play properly?

I have found a script that triggered the part and play a sound. But the sound interrupts itself if a humanoid moves around in the said part.

How do I get it so the part picks up a touch once and stops playing the sound after the humanoid leaves the selected area?

I would also like it if the sound did not interrupt itself if another humanoid were to also step in it, I want it to continue the sound until its cleared of any humanoids.

Im not sure how to fix it myself but heres what I got...

local sound = Instance.new("Sound", Workspace)
sound.Pitch = 1 
sound.SoundId = "rbxassetid://889506820"
sound.Volume = .5 
sound.Looped = true 

script.Parent.Touched:connect(function()
	sound:play()
end)

Needs fixed: Only pick up one humanoid touch. Not interrupt itself if humanoid moves around/another humanoid enters.

Can anyone help me figure out what I need to add to it?

1 answer

You need to detect if it's playing. Use Sound.IsPlaying for this:

local sound = Instance.new("Sound", Workspace)
sound.Pitch = 1 
sound.SoundId = "rbxassetid://889506820"
sound.Volume = .5 
sound.Looped = true 

script.Parent.Touched:Connect(function()
    if sound.IsPlaying == false then
        sound:Play()
    else
        print("Sound: " .. sound .. " is playing.")
    end
end)

More help can be found here.

Then, to detect if they are leaving the part use BasePart.TouchEnded like this:

local sound = Instance.new("Sound", Workspace)
sound.Pitch = 1 
sound.SoundId = "rbxassetid://889506820"
sound.Volume = .5 
sound.Looped = true 

script.Parent.Touched:Connect(function()
    if sound.IsPlaying == false then
        sound:Play()
    else
        print("Sound: " .. sound .. " is playing.")
    end
end)
script.Parent.TouchEnded:Connect(function()
      sound:Stop()
end)

More help can be found here.

2 comments

0

TouchEnded is not reliable

0

@TTChaos what else is there?

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