How to make a flickering light that dims (or flickers) more/less the louder/quieter a sound plays?

  1. What do I want to achieve? I want to have a flickering light in my game that I don’t have to write a million lines of code that main contains waits. I think dimming o brightening the light based off the volume of a sound would be much easier.

  2. What is the issue? The issue is that I have no idea how to code this at all, and I really need help on how to code the brightness of the light based off the volume of my sound.

Here’s my sound:

And here’s the light I want the sound to modify:

  1. What solutions have you tried so far? I look for some solutions on the Developer Hub and some tutorials on YouTube, although I couldn’t find anything similar to what I’m trying to do.

If anyone knows how to do this please help me. I would greatly appreciate it!

1 Like

You pretty much have to just time it.
Pause the audio at the point where the sound plays, then take the number in the TimePosition property and put that in a task.wait().
Then find the next one, take the previous time away from it, and then put that in another task.wait().

Inside the Sound Object, there’s a property called PlaybackLoudness which is a “number between 0 and 1000 indicating how loud the Sound is currently playing back”. You could then adjust the brightness of the light using bloom

local RunService = game:GetService("RunService")
local Lighting = game:GetService("Lighting")

local SoundObject = workspace.Sound

RunService.Heartbeat:Connect(function()
    local currentLoudness = SoundObject.PlaybackLoudness
    Lighting.Bloom.Intensity = currentLoudness -- You can multiply currentLoudness by a constant to adjust how much the sound affects the brightness
end)

You also might want to adjust the Size Property of the Bloom depending on the currentLoudness

1 Like

Use sound.Volume and light.Brightness

Volume is not directly linked to the amplitude of the wave at that moment. It simply just amplifies the energy of the waves.

He said he wanted to link volume and brightness, I thought he didnt know about these properties so decided to tell him…

I’m pretty sure he would have a basic idea of the properties of each instance he uses.

1 Like

Volume is static, they could however use the ‘PlaybackLoudness’ property of sound instances.
https://developer.roblox.com/en-us/api-reference/property/Sound/PlaybackLoudness

1 Like

This is close to what I was looking for, but not exactly. I’m not messing with the Lighting service in game though. I’m trying to alter this light:
image

Here are the light’s properties: (I think affecting the angle might be better than the brightness).
image

Also what type of script do I use and where to I put it?

Ah sorry, I didn’t know you were using a SpotLight.
Are you playing the sound through a server or client script?

I have the sound playing and looping through it’s properties, so I don’t know which one that would be. Sorry.

Oh ok so that’s a server I think.
Make the script a server script then, although if it doesn’t work then you can also try a local script.
Hopefully adjusting angle will give a nice effect, probably a good idea to check the print the values of PlaybackLoudness to check approximately where the maximum limit of your sound file is, and hence you can easily map the loudness to a angle.

I’ve changed the script a bit, not sure if that’s the problem. The script is inside a Script, inside ServerScriptService.

local Light = workspace.Office.Light.Bulb.SpotLight
local Sound = workspace.Office.Light.FlickeringLightSound

while wait(0.05) do
	local currentLoudness = Sound.PlaybackLoudness
	Light.Angle = currentLoudness
end

I did a print statement between the currnetLoudness and Light.Angle but it just kept saying the Loudness was 0.

It seems that the script only sets the value of the Light.Angle once.

Example:

This happened when I changed the script to this:

local Light = workspace.Office.Light.Bulb.SpotLight
local Sound = workspace.Office.Light.FlickeringLightSound

while wait(0.05) do
	local currentLoudness = Sound.PlaybackLoudness
	Light.Angle = currentLoudness + 10
end
1 Like

Well I just tested that sounds can be heard both from Server and Client so it shouldn’t’ be a problem whether it’s Server or Local script.

Can you confirm that you heard the sound?

Yeah I can hear it, I’ll try a mix of server and client side and see. What confuses me is that the script only sets the value once, even though it’s in a while wait loop.

If you add a print statement in the while loop does it stop printing?

To simplify the problem spawn a part underneath the light. Make sure you anchor it

local RunService = game:GetService("RunService")

local Part = workspace.Part
local Sound = workspace.Office.Light.FlickeringLightSound

RunService.Heartbeat:Connect(function()
    Part.Size = Vector3.new(Sound.PlaybackLoudness, 1, 1)
end)

This will show you whether the PlaybackLoudness is being read correctly so the part should react to the loudness

I believe PlaybackLoudness can only be read from the client, it doesn’t replicate to the server. You’ll have to use a local script.

That’s weird, when I was testing in studio (Run Mode), I was able to see the PlaybackLoudness have a non-zero changing value e.g. 146.15, maybe it’s different when actually running.