Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

PlaybackLoudness script not looping after the next song plays?

Hello, I have this script where it changes the fov based on PlaybackLoudness but it only works once and stops working after the next song plays. Ignore line 3 as thats for displaying the current song in a music player I have.

local randomTrack = availableMusic[math.random(1,#availableMusic)]

	currentTrack.Value = "..."

	wait(2)

	randomTrack:Play()
	
	local ScreenShakeSettings = {
		CameraMinFOV = 70,
		CameraMaxFOV = 90,
		CameraMaxVolume = 1200
	}


	game:GetService("RunService").RenderStepped:connect(function()
		local CurrentLoudness = randomTrack.PlaybackLoudness
		local FOV = ScreenShakeSettings.CameraMinFOV + (ScreenShakeSettings.CameraMaxFOV - ScreenShakeSettings.CameraMinFOV) * (CurrentLoudness / ScreenShakeSettings.CameraMaxVolume)
		if FOV > 0 and FOV < 130 then
			CurrentCamera.FieldOfView = FOV
		end
	end)

	currentTrack.Value = randomTrack.Name 

	randomTrack.Ended:Wait() 
	
end

2 comments

0

Usually in cases like yours I try putting print statements in various places to try and figure out what's running and confirm the values of variables are what I expect. Unrelated, since this is run more than once, you should be sure to disconnect the connection made (from line 16) when you're done with it (when the song ends, line 27) or else you'll end up with tons of connections always running

0

how would i disconnect it?

1 answer

It's because you did not update the randomTrack value inside the function. That's why when the song ends, the function detects nothing from the song, but another song is still playing. You could try updating the randomTrack value inside the function.

1 comment

0

Forgot to mention, above line 1 is actually a “while true do”. I just didn’t include it in when posting this topic. If that’s not what you meant, how do I achieve this?

This discussion is preserved as it was. New answers, comments, and votes are closed.