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

How can I destroy the Sound after it has played?

Asked by 10 years ago

Hello. I have a music script that plays three songs in a row, but after a while I end up with a lot of sound files in the workspace because they don't get removed when they are finished playing. This is a problem because who ever joins after a while will get all of the sounds playing at once. Here is my script:

while true do



local s1 = Instance.new("Sound")

s1.Name = "Sound1"
s1.SoundId = "http://www.roblox.com/asset/?id=162613575" 
s1.Volume = 2                                                  --SONG 1
s1.Pitch = 1 
s1.Looped = false
s1.archivable = false

s1.Parent = game.Workspace

wait(0)

s1:play()

wait(120) --waits until the next song plays


local s2 = Instance.new("Sound")

s2.Name = "Sound2"
s2.SoundId = "http://www.roblox.com/asset/?id=162614272" 
s2.Volume = 2 
s2.Pitch = 1                                                    --SONG 2
s2.Looped = false
s2.archivable = false

s2.Parent = game.Workspace

wait(0)

s2:play()

wait(120) --waits until the next song plays



local s3 = Instance.new("Sound")

s3.Name = "Sound3"
s3.SoundId = "http://www.roblox.com/asset/?id=162615529" 
s3.Volume = 2 
s3.Pitch = 1                                                    --SONG 3
s3.Looped = false
s3.archivable = false

s3.Parent = game.Workspace

wait(0)

s3:play()

wait(120) --waits until the next song plays

s1:play()

end

I am wondering how I can remove the "Sound" object before a new one comes in. I have tried Workspace.Sound1:Destroy after each, just changing the number after what the object is called. Hopefully someone can help me.

3 answers

Log in to vote
1
Answered by 10 years ago

There is a property in the sounds called PlayOnRemove

I edited your script, so that when the audio is destroyed, the audio will play, and workspace wont be spammed with sound's

while true do



local s1 = Instance.new("Sound")

s1.Name = "Sound1"
s1.SoundId = "http://www.roblox.com/asset/?id=162613575" 
s1.Volume = 2                                                  --SONG 1
s1.Pitch = 1 
s1.Looped = false
s1.archivable = false

s1.Parent = game.Workspace
s1.PlayOnRemove=true
wait(0)

s1:Destroy()

wait(120) --waits until the next song plays


local s2 = Instance.new("Sound")

s2.Name = "Sound2"
s2.SoundId = "http://www.roblox.com/asset/?id=162614272" 
s2.Volume = 2 
s2.Pitch = 1                                                    --SONG 2
s2.Looped = false
s2.archivable = false

s2.Parent = game.Workspace
s2.PlayOnRemove=true
wait(0)

s2:Destroy()

wait(120) --waits until the next song plays



local s3 = Instance.new("Sound")

s3.Name = "Sound3"
s3.SoundId = "http://www.roblox.com/asset/?id=162615529" 
s3.Volume = 2 
s3.Pitch = 1                                                    --SONG 3
s3.Looped = false
s3.archivable = false

s3.Parent = game.Workspace
s3.PlayOnRemove=true
wait(0)

s3:Destroy()

wait(120) --waits until the next song plays

--Beacuse we have the while loop and it makes a new audio, no need for the play that was right there.

end

Basically it will loop like this

  1. Create Audio 1 (And set propeties)

  2. Destroy the audio, playing on it being removed.

  3. Create Audio 2

  4. Destroy+Play

  5. Create Audio 3

  6. Destroy+Play

    Repeat

Ad
Log in to vote
-2
Answered by 10 years ago

--I added a few lines of code removing the song after it's time is up. while true do

local s1 = Instance.new("Sound")

s1.Name = "Sound1" s1.SoundId = "http://www.roblox.com/asset/?id=162613575" s1.Volume = 2 --SONG 1 s1.Pitch = 1 s1.Looped = false s1.archivable = false

s1.Parent = game.Workspace

wait(0)

s1:play()

wait(120) --waits until the next song plays

s1:stop() s1:destroy()

local s2 = Instance.new("Sound")

s2.Name = "Sound2" s2.SoundId = "http://www.roblox.com/asset/?id=162614272" s2.Volume = 2 s2.Pitch = 1 --SONG 2 s2.Looped = false s2.archivable = false

s2.Parent = game.Workspace

wait(0)

s2:play()

wait(120) --waits until the next song plays

s2:stop() s2:destroy()

local s3 = Instance.new("Sound")

s3.Name = "Sound3" s3.SoundId = "http://www.roblox.com/asset/?id=162615529" s3.Volume = 2 s3.Pitch = 1 --SONG 3 s3.Looped = false s3.archivable = false

s3.Parent = game.Workspace

wait(0)

s3:play()

wait(120) --waits until the next song plays

s3:stop() s3:destroy()

end

Log in to vote
-2
Answered by 10 years ago

Use either Game.Workspace.SoundNameHere:Remove() or Use either Game.Workspace.SoundNameHere:Destroy()

Answer this question