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

How would you change playback speed of all sounds at once?

Asked by 1 year ago

Essentially I need help for a game I'm working on. I need all playback speeds in the game to be slowed down at times. Many games and models do this. If you've played Null Work (open the roblox menu) you'd get what I mean.

I want to do the same thing with my game. This is the current code:

local GuiService = game:GetService('GuiService')
repeat wait() until workspace.CurrentCamera
local Camera = workspace.CurrentCamera
local Blur,Color = Instance.new('BlurEffect'),Instance.new('ColorCorrectionEffect')
Blur.Size = 25 Blur.Name = 'ESCB' Color.Saturation = -.5 Color.Name = 'ESCC'

GuiService.MenuOpened:Connect(function()
    local NewBlur,NewColor = Blur:Clone(),Color:Clone()
    NewBlur.Parent,NewColor.Parent = Camera,Camera
end)
GuiService.MenuClosed:Connect(function()
    for i,v in pairs(Camera:GetChildren()) do
        if v.Name == 'ESCB' or v.Name == 'ESCC' then
            v:Destroy()
        end
    end
end)

When the menu opens, it blurs. I just need all sounds to slow down too. Help is majorly appreciated!

1 answer

Log in to vote
0
Answered by
174gb 290 Moderation Voter
1 year ago
Edited 1 year ago
--First you will need have the default playback speed of each sound, so we make a table and store them
local PlaybackSpeeds = {}

local function StorePlaybackSpeeds()
    for i, v in game:GetDescendants() do
        if not v:IsA("Sound") then continue end
        if PlaybackSpeeds[v] == nil then
            PlaybackSpeeds[v] = v.PlaybackSpeed
        end
    end
end

--Then make a function to change the playback speed of all sounds in game
local function ChangePlaybackSpeeds(NewPlaybackSpeed)
    for i, v in game:GetDescendants() do
        if not v:IsA("Sound") then continue end
        v.PlaybackSpeed = typeof(NewPlaybackSpeed) == 'number' and NewPlaybackSpeed or PlaybackSpeeds[v] 
    end
end

StorePlaybackSpeeds() -- Call the function to store the default playback speed values

-- Then are just call the functions when the Menu Opens / Close
ChangePlaybackSpeeds(0.5) -- Change the playback speed to 0.5
ChangePlaybackSpeeds('Default') -- Change the playback speed to default value

Here's the implementation on your code

local GuiService = game:GetService('GuiService')
repeat wait() until workspace.CurrentCamera
local Camera = workspace.CurrentCamera
local Blur,Color = Instance.new('BlurEffect'),Instance.new('ColorCorrectionEffect')
Blur.Size = 25 Blur.Name = 'ESCB' Color.Saturation = -.5 Color.Name = 'ESCC'

local PlaybackSpeeds = {}

local function StorePlaybackSpeeds()
    for i, v in game:GetDescendants() do
        if not v:IsA("Sound") then continue end
        if PlaybackSpeeds[v] == nil then
            PlaybackSpeeds[v] = v.PlaybackSpeed
        end
    end
end

local function ChangePlaybackSpeeds(NewPlaybackSpeed)
    for i, v in game:GetDescendants() do
        if not v:IsA("Sound") then continue end
        v.PlaybackSpeed = typeof(NewPlaybackSpeed) == 'number' and NewPlaybackSpeed or PlaybackSpeeds[v] 
    end
end

GuiService.MenuOpened:Connect(function()
    local NewBlur,NewColor = Blur:Clone(),Color:Clone()
    NewBlur.Parent,NewColor.Parent = Camera,Camera
   ChangePlaybackSpeeds(0.5)
end)

GuiService.MenuClosed:Connect(function()
    for i,v in pairs(Camera:GetChildren()) do
        if v.Name == 'ESCB' or v.Name == 'ESCC' then
            v:Destroy()
        end
    end
   ChangePlaybackSpeeds('Default')
end)

StorePlaybackSpeeds() 
Ad

Answer this question