How to make a part pulse with music?

I want to achieve the effect of a simple part with some transparency to pulse its transparency with a sound and beat. I was wondering how I can go about creating this as I am not sure how I would be able to so.

Loudness Based

In this case, My first take would be that I’d utilize PlaybackLoudness of Sound which will be wrapped in RunService’s Heartbeat which is an event that fires every frame (kind of like doing while task.wait() do but much better). It seems like the maximum Roblox can receive the loudness is 1000, so you could perhaps do x/1000 to get the percentage and then make it the transparency (and if you want, add/subtract the transparency). It is unclear whether you’re trying to achieve this server or client sided.

Beat-wised Based

This is fairly easy. I would just get seconds per beat by doing BPM/60 and do while task.wait(SPB) do and use TweenService to linearly control the transparency with the duration of SPB.

I’m kind of inexperienced, so my ways might be inefficient

Any chance I can get a code example also?

Beat-wised or Loudness based? I’d rather do this for localscript.

Beat wised I think but loudness may work. I am just trying to get the part to pulse to the sound and as long as it can work correctly for any and every sound, and actually look like it is pulsing to the sounds on time and correctly then I think that is what I am looking to go for

So I’ve realized that Heartbeat’s not the most effective way to do this. I forgot the existence of RenderStepped yesterday, so I’m going to use it today for this code.

Here’s an example for loudness

local runService = game:GetService("RunService")
local sound = workspace.Sound
local part = workspace.Part

RunService.RenderStepped:Connect(function()
    local loudness = sound.PlaybackLoudness
    local percentLoud = loudness/1000 -- You can change the 1000 to be the maximum of any
    local invertRes = 1 - percentLoud -- Makes it so the result returns inverted so it can be transparent a low value and opaque at high value
    part.Transparency = invertRes
end)

I haven’t debug the script, so it’s likely recommended not to be used in the final project. Though, that’s a way on how you script it for example.

3 Likes

This will work. Thank you for your help

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.