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

How do you remove one Sound instance if there's more then 1 in workspace?

Asked by
KenzaXI 166
9 years ago

hi, I've made this script, But I want to delete any extra sound instances. there should be a maximum of 1 Sound instance.

local Clicker = script.Parent
function onClick()
    local Music = Instance.new("Sound", game.Workspace)
    Music.SoundId = "https://www.roblox.com/asset/?id=229710755"
    Music.Looped = false
    Music.Archivable = false
    wait(0)
    Music:Play()
    wait(120)
    Music:Stop()
    if game.Workspace.Sound == true then
        game.Workspace.Sound:Remove()
    end
end
Clicker.ClickDetector.MouseClick:connect(onClick)

any help will be appreciated.

1
The script works, change line 11 to (if game.Workspace:FindFirstChild("Sound") then) EzraNehemiah_TF2 3552 — 9y
0
That didn't work, but thanks for helping KenzaXI 166 — 9y
0
Oh, if there is more than one, I didn't read the whole thing EzraNehemiah_TF2 3552 — 9y

4 answers

Log in to vote
1
Answered by 9 years ago

Instead of deleting your old music and adding in a new one, insert a sound object and change the sound ID when you want to change the music and then use the Play function/method.

local Music = Instance.new("Sound", workspace)
if workspace:FindFirstChild("SpecialMusic") then
    Music:Destroy()
else
    Music.Name = "SpecialMusic"
end
Music.Looped = false

script.Parent.ClickDetector.MouseClick:connect(function(plyr)
    Music.SoundId = "https://www.roblox.com/asset/?id=229710755"
    Music:Play()
    wait(120)
    Music:Stop()
end)

If there are already other sound IDs in the game:

temporarily = true --If this is true, then all other playing music in workspace is going to pause then play again. If it is set to false, the music will be off FOREVER!

function revive(audio)
    wait(120)
    sound:Play()
end

script.Parent.ClickDetector.MouseClick:connect(function(plyr)
    local Music = Instance.new("Sound", workspace)
    Music.SoundId = "https://www.roblox.com/asset/?id=229710755"
    Music.Looped = false
    for _,sound in pairs(workspace:GetChildren()) do
        if sound:IsA("Sound") and sound ~= Music then
            if temporarily then
                revive(sound)
            else
                sound:Destroy()
            end
        end
    end
    Music:Play()
    wait(120)
    Music:Destroy()
end


Hope this helps! If there is something that is not right please tell me!

Ad
Log in to vote
0
Answered by
KenzaXI 166
9 years ago

Thanks for helping, but unfortunately none of your scripts worked like the way I wanted them too, so I created a new part which Mutes the sound and removes it. again thanks for all your help

Log in to vote
-1
Answered by
wackem 50
9 years ago

insert this:

v = game.Workspace:GetChildren() if v:IsA("Sound") then v:Destroy() end

0
I'm sorry if that doesn't work, I usually don't play with sounds wackem 50 — 9y
Log in to vote
-1
Answered by
Drogb4 15
9 years ago

Okay so the function only runs once right? So this means it will only remove the first Sound it finds, what you might want to do is use i, v in pairs() loop. So it can look through all the workspace children and if there is a sound it will destroy it.

If you have no experience with in pairs loop i'd recommend you to check the wiki.

for i, v in pairs(game.Workspace:children()) do
    if v:IsA("Sound") then
        v:Remove()
    end
end
Clicker.ClickDetector.MouseClick:connect(onClick)

Hope this helped!

0
It somewhat worked, I want it so if there is more than 1 then it removes the extra sounds, I need it so there is a maximum of 1 sound playing KenzaXI 166 — 9y

Answer this question