Function for cameraFOV visualizer disconnects after next song is played (playlist)

Hi there, I have a cameraFOV script that visualizes and basically alters the FOV based of the loudness of the song. The issue is, is that after the song is stopped, and the next song is played, the function doesn’t reconnect. I’ve tried making it so that when the song stops and then the next song plays, the function disconnects and then connects again but however to no appeal.

local RunService = game:GetService("RunService")
local Button = script.Parent
local musicplayer = workspace.Sound
local CurrentCamera = workspace.CurrentCamera

local ScreenShakeSettings = {
	CameraMinFOV = 70,
	CameraMaxFOV = 80,
	CameraMaxVolume = 1700
}

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

if musicplayer:Stop() then
	RunService:UnbindFromRenderStep()
end
1 Like

I believe what you’re looking for is the Stopped Event of Sound Objects, I don’t think you can sanity check for a function or not unless if you use pcalls

local RunService = game:GetService("RunService")
local Button = script.Parent
local musicplayer = workspace.Sound
local CurrentCamera = workspace.CurrentCamera

local ScreenShakeSettings = {
	CameraMinFOV = 70,
	CameraMaxFOV = 80,
	CameraMaxVolume = 1700
}

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

musicplayer.Stopped:Connect(function()
	RunService:UnbindFromRenderStep()
end)
1 Like

Alright I see. What do you mean by sanity check?

This line specifically here, I don’t think you can bound function objects to BindToRenderStep but you can with custom functions, although looking at the API for UnbindFromRenderStep() it seems like you haven’t put any parameters onto it hence why it doesn’t reconnect the "CurrentTrack" function

Perhaps try this?

local RunService = game:GetService("RunService")
local Button = script.Parent
local musicplayer = workspace.Sound
local CurrentCamera = workspace.CurrentCamera

local ScreenShakeSettings = {
	CameraMinFOV = 70,
	CameraMaxFOV = 80,
	CameraMaxVolume = 1700
}

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

musicplayer.Stopped:Connect(function()
    local success, whoops = pcall(function()
	    RunService:UnbindFromRenderStep("CurrentTrack")
    end)

    if success then
        print("Successfully stopped song!")
    else
        warn("Oops! Something happened.", whoops)
    end
end)

Another thing is that are you checking for when the Song starts to replay again? Cause that might be another issue we haven’t looked into, as this would only run once & that’s it

disregard that comment i just posted, i put in the wrong script

It’s fine, I do believe though that this would only run once hence why you’d need to check when to exactly bind & unbind the functions by using Sound Events, such as Stopped & Played

If the script doesn’t work, try this one:

local RunService = game:GetService("RunService")
local Button = script.Parent
local musicplayer = workspace.Sound
local CurrentCamera = workspace.CurrentCamera

local ScreenShakeSettings = {
	CameraMinFOV = 70,
	CameraMaxFOV = 80,
	CameraMaxVolume = 1700
}

musicplayer.Played:Connect(function()
    game:GetService("RunService"):BindToRenderStep("CurrentTrack", 1, function()
	    local CurrentLoudness = musicplayer.PlaybackLoudness
	    local FOV = ScreenShakeSettings.CameraMinFOV + (ScreenShakeSettings.CameraMaxFOV - ScreenShakeSettings.CameraMinFOV) * (CurrentLoudness / ScreenShakeSettings.CameraMaxVolume)
	    if FOV > 0 and FOV < 130 then
		    CurrentCamera.FieldOfView = FOV
	    end
    end)
end)

musicplayer.Stopped:Connect(function()
    local success, whoops = pcall(function()
	    RunService:UnbindFromRenderStep("CurrentTrack")
    end)

    if success then
        print("Successfully stopped song!")
    else
        warn("Oops! Something happened.", whoops)
    end
end)
``

This time, when I play the game on studio, the function doesn’t run on default. What I’ll try is do something like when music is stopped, it disconnects and then connects after that

I did something different and a bit simpler to explain my idea more.

Something like this?

local RunService = game:GetService("RunService")
local Button = script.Parent
local musicplayer = workspace.Sound
local CurrentCamera = workspace.CurrentCamera

local ScreenShakeSettings = {
	CameraMinFOV = 70,
	CameraMaxFOV = 80,
	CameraMaxVolume = 1700
}

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

musicplayer.Stopped:Connect(function()
	RunService:UnbindFromRenderStep("CurrentTrack")
end)

musicplayer.Played:Connect(function()
	RunService:BindToRenderStep("CurrentTrack")
end)

Yeah I can kinda see, I haven’t touched much on BindToRenderStep but I think with a bit of debugging and functions we can make this work

local RunService = game:GetService("RunService")
local Button = script.Parent
local musicplayer = workspace.Sound
local CurrentCamera = workspace.CurrentCamera

local ScreenShakeSettings = {
	CameraMinFOV = 70,
	CameraMaxFOV = 80,
	CameraMaxVolume = 1700
}

local function EnableVisualizer()
    print("Visualizer Enabled") --This will prob repeat a lot of times

	local CurrentLoudness = musicplayer.PlaybackLoudness
	local FOV = ScreenShakeSettings.CameraMinFOV + (ScreenShakeSettings.CameraMaxFOV - ScreenShakeSettings.CameraMinFOV) * (CurrentLoudness / ScreenShakeSettings.CameraMaxVolume)

    if FOV > 0 and FOV < 130 then
		CurrentCamera.FieldOfView = FOV
	end
end
musicplayer.Played:Connect(function()
    print("Current Song Now Playing")
    RunService:BindToRenderStep("CurrentTrack", 1, EnableVisualizer)
end)

musicplayer.Stopped:Connect(function()
    print("Current Song Stopped")

    local success, whoops = pcall(function()
	    RunService:UnbindFromRenderStep("CurrentTrack")
    end)

    if success then
        print("Successfully stopped song!")
    else
        warn("Oops! Something happened.", whoops)
    end
end)

RunService:BindToRenderStep("CurrentTrack", 1, EnableVisualizer)

Not entirely sure if this would work but check your Output & see what pops up

Holy crap. I’m going to have to remove the print for that one line since it’s lagging my entire game lol.

That seemed to work. Thank you so much! I’ll read through the script and understand each line.

1 Like