Synchronizing lights with sound (music)

Hey! I’m trying to make a light show and firework display live event. It looks good so far, however, I’d like to properly synchronize it with the sound (music), so that the lights change how they really should and don’t depend on just wait or loop functions.

Any idea how I can easily do this? I never really worked with synchronizing stuff with sounds.

1 Like

you need to match the light change to the song bpm

1 Like

Any idea why the light doesn’t do anything?

local runS = game:GetService("RunService")

local audio = game:GetService("SoundService"):WaitForChild("EventMusic")

local bpm = 140
local secPerBeat = 0
local songPosition = 0
local songPositionInBeats = 0
local firstBeatOffset = 0
local dspSongTime = 0

function Start()
	secPerBeat = 60.0 / bpm
	dspSongTime = audio.TimePosition

	audio:Play()
end

function Update()
	songPosition = audio.TimePosition - dspSongTime - firstBeatOffset
	songPositionInBeats = songPosition / secPerBeat

	script:SetAttribute("songPosition", songPosition)
	script:SetAttribute("songPositionInBeats", songPositionInBeats)
end

Start()
runS.Heartbeat:Connect(Update)

local runS = game:GetService("RunService")

local conductor = script.Parent.Conductor

local eventFireTime = 1
local eventFireTime2 = 2

runS.Heartbeat:Connect(function()
	if eventFireTime <= conductor:GetAttribute("songPositionInBeats") then
		local WashLightsModel = workspace:WaitForChild("WashLights")
		local Light = WashLightsModel:FindFirstChild("WashLight")
		Light.Head.Lens.Transparency = 0
		Light.Head.Lens.Beam1.Enabled = true
		Light.Head.Lens.SurfaceLight.Enabled = true
	end
	if eventFireTime2 <= conductor:GetAttribute("songPositionInBeats") then
		local WashLightsModel = workspace:WaitForChild("WashLights")
		local Light = WashLightsModel:FindFirstChild("WashLight")
		Light.Head.Lens.Transparency = 1
		Light.Head.Lens.Beam1.Enabled = false
		Light.Head.Lens.SurfaceLight.Enabled = false
	end
end)