Audio visualizer for gui not working

hello i made an audio visualzier script for my gui but its not working.
i want the frame to change size with the loudeness of the audio constantly.

heres the script:

while task.wait(0.01) do

print("Running")

script.Parent.Size = UDim2.new(((math.clamp(game.SoundService.Sound.PlaybackLoudness, 0, 1)) / 30), 0, 1, 2)

end

script:

so i want it to expand:

but it does this:

image

1 Like

Ok. Firstly you’re going to need to make sure your frame/imagelabel has a “Anchor Point”. reposition your Position + Anchor point until its in the exact same positioning. This will make it so when you change its size it will maintain its current position allowing it to visually expand/shrink in all directions vs XY positive.

Reading your UDim2 the behaviour is set like this:
You’ve given it an offset of 2 and scale of 1 on the Y axis meaning it’ll always be that length on the Y axis
Reading playback loudness nothing seems to be wrong. Reply once you’ve set an anchor point and position as theirs most likely a more to be done.
image

1 Like

Adding on to Vellian’s reply:

  • Set the anchor point to <0.5, 0.5>, then position the circle in the middle. This makes the circle stay in the middle when it resizes.
  • UDim2s are formatted like this: scale x, offset x, scale y, offset y. You want to set both the scale x and the scale y. To do this, try something like this to set the size:
local scale = math.clamp(game.SoundService.Sound.PlaybackLoudness, 0, 1) / 30
script.Parent.Size = UDim2.new(scale, 0, scale, 0)
  • In addition to this, make sure 30 is your max loudness and that the sound is playing

Edit:

Nice catch! The code should be:

local scale = math.clamp(game.SoundService.Sound.PlaybackLoudness / 30, 0, 1)

not

local scale = math.clamp(game.SoundService.Sound.PlaybackLoudness, 0, 1) / 30
1 Like

Everyone else missed this, but you’re clamping (meaning it just returns the max clamp if it’s higher) your loudness. You’re not mapping it. If you want to map it to 0-1, divide it by 1000. Some sounds (especially if you have audio effects under them) go higher than 1000, so maybe math.max(MaxLoudness, CurrentLoudness) and divide by MaxLoudness.

1 Like

ok so i did some play testing and found a solution. however it was really choppy and i want it so that the size moves to the newer one not just teleports. what i did is im using a for loop to loop through until it hits the newer point. my only error now is im comparing a UDIM to a number however i dont know a fix to this

heres script:


wait(5)
while true do
	local loud = game.SoundService.Sound.PlaybackLoudness
	local frame = script.Parent.UIStroke
	task.wait(0.1)
	local pos = script.Parent.Position
	if loud > 250 then -- if sound is under 250 (So its not to small)
		local size = loud/450 --devide sound by 450

		if size > script.Parent.Size.X then -- if the numbers bigger than the size of the frame then use addition to get to there
			for i=size, script.Parent.Size.X, 0.1 do
				wait(0.01)
				script.Parent.Size = script.Parent.Size + UDim2.new(0.1,0.1,0.1,0.1)
				script.Parent.Position = pos
				if size == script.Parent.Size then break
				else print(size)		
				end
			end
		elseif size < script.Parent.Size.X then -- if the numbers smaller than the size of the frame then use subtraction to get to there
			for i=size, script.Parent.Size.X, -0.1 do
				wait(0.05)
				script.Parent.Size = script.Parent.Size - UDim2.new(0.1,0.1,0.1,0.1)
				script.Parent.Position = pos
				if size == script.Parent.Size then break
				else print(size)
				end
			end
		else	--if its to small just put it to minimum size	
			script.Parent.Size = UDim2.new(0.7, 0.7, 0.7, 0.7) 
			script.Parent.Position = pos
		end
	end
end

error:

Players.SakDevRblx.PlayerGui.ScreenGui.Music.Frame.ImageLabel.LocalScript:10: attempt to compare UDim < number
1 Like
wait(5)
while true do
	local loud = game.SoundService.Sound.PlaybackLoudness
	local frame = script.Parent.UIStroke
	task.wait(0.05)
	local pos = script.Parent.Position
	if loud > 100 then
		local size = loud/400
		local willTween = script.Parent:TweenSize(
			UDim2.new(size, size, size, size),  -- endSize (required)
			Enum.EasingDirection.Out,    -- easingDirection (default Out)
			Enum.EasingStyle.Quad,      -- easingStyle (default Quad)
			0.05,                          -- time (default: 1)
			true)  
		script.Parent.Position = pos
	else
		local willTween = script.Parent:TweenSize(
			UDim2.new(0.7, 0.7, 0.7, 0.7),  -- endSize (required)
			Enum.EasingDirection.Out,    -- easingDirection (default Out)
			Enum.EasingStyle.Quad,      -- easingStyle (default Quad)
			0.05,                          -- time (default: 1)
			true)  
		script.Parent.Position = pos
	end
end```


                   
this is the solution bye guys!
1 Like