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.
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.
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!
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
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.
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
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.
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
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.