How do you stop a function (sounds) from playing during another function?

Hey developers, hitting the devforum again with another good question to have in knowledge:

How can you stop any sounds from a sound folder playing DURING an entire function, then when the function ends to allow for the sounds to play if scripted to?

In further explanation, I have a reactor core as an example;
There are 2 functions:

local function meltdown()

local function shutdown()

During the shutdown process any sounds from the meltdown process is to be halted. Usually it will continue as normal while another function goes along, and for example if you’re setting a function inside the meltdown function itself, it will do just the same.

So, in a nutshell:

Function starting = sounds to stop playing
Function ending = sounds to be ALLOWED to play, however not to play

I use getdescendants on a basis to stop sounds from playing, which works, however it only does it once, where I would like to do it on a consecutive loop for the entire function.

Thanks, and I hope to get some good answers soon!

I didn’t quite understand what you mean but I think something like this:

-- [code not tested]
local soundsFolder = -- set the sounds folder

local function starting()
    for i, v in pairs(soundsFolder) do
        if v:IsA("Sound") then -- you can add a filter to stop just some sounds if you want to, like: if v:IsA("Sound") and v.Name == "soundToStop" then
            v:Stop()
        end
    end
    -- your code here
end

local function shutdown()
    for i, v in pairs(soundsFolder) do
        if v:IsA("Sound") then -- you can add a filter to play just some sounds if you want to, like: if v:IsA("Sound") and v.Name == "soundToPlay" then
            v:Play()
        end
    end
    -- your code here
end

Yes, but how would I prevent the sound from playing in the future?

If you have no other functions that start the sound it should not start, in case you could put a check to see if the function can start it, like:

local sound = true -- in the starting put sound = false and in the shutdown put sound = true

if sound then
    MusicPlayer:Play()
end

another solution, although not the most elegant, is to set the volume to 0

Because my common knowledge is terrible, setting the volume to 0 and then changing it later will do.

1 Like