GUI frames to change to a random Y size depending on audio PlaybackLoudness?

How could I make a GUI frame to change its Y size depending on audio PlaybackLoudness?

This is straight forward when you think about how we can utilise and factor in properties into our implementations.

We know we can set the size of UI elements through code, so all that we need to do is set the frame’s Y size (offset) to the current playback loudness!


local RunService = game:GetService("RunService")
local HeartbeatStep = RunService.Heartbeat

local Frame = script.Parent:WaitForChild("Frame")
local Sound = script.Parent:WaitForChild("Sound")

while HeartbeatStep:Wait() do
   -- I've inverted the value here since my frame for testing was at the bottom of the screen
   -- you may need to change this code slightly to make it work ideally! :)
   Frame.Size = UDim2.new(0,50, 0, -Sound.PlaybackLoudness)
end
1 Like

How would I make it so other frames don’t share the exact same Y size?

I see, so you want a certain audio bar frame to represent the exact playback loudness and then other frames to be lower in height to mimic the effect of different soundwaves.

You can generate a random integer and then deduct this from the original PlaybackLoudness.

local PlaybackLoudness = Sound.PlaybackLoudness
local Divider = 2 --if 1, you run the risk of deducting the frame's entire height and having an invisible bar
local RandomDeduction = math.random((PlaybackLoudness / Divider))

local LowerFrameSize = UDim2.new(0,50,0,-(PlaybackLoudness - RandomDeduction))

Of course, you would then repeat this if in a loop and each time it’d be a completely random deduction from the Y size. :+1: