How do I disconnect/connect a function based of a text button

How do I make it so my fov function disconnects/connects on a click of a text button? I’ve got the function inside a music playlist and player script that both displays the song playing.

while true do 

	for i = 1, #music do

		local currentmusic = music[i]
		local SongID = string.match(currentmusic, "%d+")
		local SongInfo = MPS:GetProductInfo(SongID)

		musicplayer.SoundId = currentmusic 
		musicplayer:Play() 

		currentTrack.Value = "..." -- displays the song playing in a music player gui

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

		connection = game:GetService("RunService").RenderStepped:connect(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)

		currentTrack.Value = SongInfo.Name -- just for displaying current song playing

		musicplayer.Ended:Wait()

		connection:Disconnect()

	end
end
1 Like

In this case it would be ideal to use
RunService:BindToRenderStep(name, priority, function)
You can then unbind the respective binded functions name on the click of a button using
RunService:UnbindFromRenderStep(name)

1 Like

What would be an example of this being inside my script?

1 Like

In your case it would look something like this:

while true do 

	for i = 1, #music do

		local currentmusic = music[i]
		local SongID = string.match(currentmusic, "%d+")
		local SongInfo = MPS:GetProductInfo(SongID)

		musicplayer.SoundId = currentmusic 
		musicplayer:Play() 

		currentTrack.Value = "..." -- displays the song playing in a music player gui

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

		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)

		currentTrack.Value = SongInfo.Name -- just for displaying current song playing

		musicplayer.Ended:Wait()

		game:GetService("RunService"):UnbindFromRenderStep("CurrentTrack")

	end
end

I just noticed your including the “CurrentTrack”. I only want the fov playbackloudness to be unbinded on a click of a button, not the currenttrackvalue too

1 Like

CurrentTrack is just the name for the function binded to RenderStep, it does not really matter what you call it, just as long as you also unbind the same name

Oh, okay. I may sound a bit spoiled but what is actually happening in the script? This whole script is just actually a local music script from starterplayerscripts. Can you please explain how this would and how it to work if this was from a button, from another local script? I’m new to lua sorry.

I see you have a table of SoundID’s that are being played under a Sound Instance, the camera is then being distorted every frame, depending on the PlaybackLoudness. If I’m not wrong, I’m pretty sure you can call RunService:UnbindFromRenderStep(name)from any local script, local to the player so you would simply have to connect to the button click event like and Unbind like so:

local RunService = game:GetService("RunService")
local Button = ...

local function UnbindCameraShake()
  RunService:UnbindFromRenderStep("CurrentTrack")
end

Button.MouseButton1Click:Connect(UnbindCameraShake)

Is this in a new local script under the textbutton or inside the script before?

This could be under the TextButton, you would simply have to correctly reference the button like so

local RunService = game:GetService("RunService")
local Button = script.Parent

local function UnbindCameraShake()
  RunService:UnbindFromRenderStep("CurrentTrack")
end

Button.MouseButton1Click:Connect(UnbindCameraShake)

How do I make it so that when I click again, it turns on the function?